Profiler.cpp
Go to the documentation of this file.
00001 /*      
00002  * File: Profile.cpp
00003  * Project: DUtils library
00004  * Author: Dorian Galvez-Lopez
00005  * Date: September 14, 2010
00006  * Description: class for profiling code
00007  *
00008  * 
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  */
00023 
00024 #include <map>
00025 #include <vector>
00026 #include <string>
00027 #include <algorithm>
00028 
00029 #include "Timestamp.h"
00030 #include "Profiler.h"
00031 #include "Math.hpp"
00032 
00033 using namespace DUtils;
00034 using namespace std;
00035 
00036 // ---------------------------------------------------------------------------
00037 
00038 void Profiler::profile(const std::string &name)
00039 {
00040   Timestamp t;
00041   m_last_profile = name;
00042   pair<std::map<std::string, Timestamp>::iterator, bool> res =
00043     m_start_points.insert(make_pair(name, t));
00044   res.first->second.setToCurrentTime();
00045 }
00046 
00047 // ---------------------------------------------------------------------------
00048 
00049 void Profiler::stopAndScale(double scale, const std::string &name)
00050 {
00051   Timestamp t;
00052   t.setToCurrentTime();
00053   
00054   string s = (name.empty() ? m_last_profile : name);
00055   
00056   std::map<std::string, Timestamp>::iterator it = m_start_points.find(s);
00057   
00058   if(it != m_start_points.end())
00059   {
00060     double duration = (t - it->second) * scale;
00061     m_start_points.erase(it);
00062     
00063     pair<std::map<std::string, std::vector<double> >::iterator,bool> pdit;
00064     pdit.first = m_profiles.find(s);
00065     if(pdit.first == m_profiles.end())
00066     {
00067       pdit = m_profiles.insert(make_pair(s, vector<double>()));
00068     }
00069     pdit.first->second.push_back(duration);
00070   }
00071 }
00072 
00073 // ---------------------------------------------------------------------------
00074 
00075 double Profiler::getMeanTime(const std::string &name) const
00076 {
00077   std::map<std::string, std::vector<double> >::const_iterator it =
00078     m_profiles.find(name);
00079   
00080   if(it != m_profiles.end())
00081   {
00082     return Math::Mean<double>(it->second);
00083   }
00084   else return 0;
00085 }
00086 
00087 // ---------------------------------------------------------------------------
00088 
00089 double Profiler::getStdevTime(const std::string &name) const
00090 {
00091   std::map<std::string, std::vector<double> >::const_iterator it =
00092     m_profiles.find(name);
00093 
00094   if(it != m_profiles.end())
00095   {
00096     return Math::Stdev<double>(it->second);
00097   }
00098   else return 0;
00099 }
00100   
00101 // ---------------------------------------------------------------------------
00102   
00103 double Profiler::getMinTime(const std::string &name) const
00104 {
00105   std::map<std::string, std::vector<double> >::const_iterator it =
00106     m_profiles.find(name);
00107   
00108   if(it != m_profiles.end())
00109   {
00110     return *std::min_element(it->second.begin(), it->second.end());
00111   }
00112   else return 0;
00113 }
00114   
00115 // ---------------------------------------------------------------------------
00116   
00117 double Profiler::getMaxTime(const std::string &name) const
00118 {
00119   std::map<std::string, std::vector<double> >::const_iterator it =
00120     m_profiles.find(name);
00121   
00122   if(it != m_profiles.end())
00123   {
00124     return *std::max_element(it->second.begin(), it->second.end());
00125   }
00126   else return 0;
00127 }
00128   
00129 // ---------------------------------------------------------------------------
00130   
00131 void
00132 Profiler::getTime(std::vector<double> &time, const std::string &name) const
00133 {
00134   std::map<std::string, std::vector<double> >::const_iterator it =
00135     m_profiles.find(name);
00136   
00137   if(it != m_profiles.end())
00138   {
00139     time = it->second;
00140   }
00141   else time.clear();
00142 }
00143   
00144 // ---------------------------------------------------------------------------
00145 
00146 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:13