35#include "crocoddyl/core/utils/stop-watch.hpp" 
   38using std::ostringstream;
 
   43Stopwatch& getProfiler() {
 
   44  static Stopwatch s(REAL_TIME);  
 
   49    : active(true), mode(_mode), profiler_active(false) {
 
   50  records_of = 
new map<string, PerformanceData>();
 
 
   66  if (
mode == CPU_TIME) {
 
   68    return (
long double)clock();
 
   69  } 
else if (
mode == REAL_TIME) {
 
   75    LARGE_INTEGER intervals;
 
   79    GetSystemTimeAsFileTime(&ft);
 
   80    intervals.LowPart = ft.dwLowDateTime;
 
   81    intervals.HighPart = ft.dwHighDateTime;
 
   83    long double measure = intervals.QuadPart;
 
   84    measure -= 116444736000000000.0;  
 
   85    measure /= 10000000.0;            
 
   91    gettimeofday(&tv, NULL);
 
   93    long double measure = tv.tv_usec;
 
   95    measure += (
long double)tv.tv_sec;  
 
 
  106    const string& perf_name) {
 
  107  const auto it = 
records_of->insert(make_pair(perf_name, PerformanceData()));
 
  108  return it.first->second;
 
  137  stop_perf(perf_info, clock_end);
 
 
  140void Stopwatch::stop_perf(PerformanceData& perf_info, 
long double clock_end) {
 
  142  if (perf_info.clock_start == 0) 
return;
 
  145  long double lapse = clock_end - perf_info.clock_start;
 
  147  if (
mode == CPU_TIME) lapse /= (double)CLOCKS_PER_SEC;
 
  150  perf_info.last_time = lapse;
 
  153  if (lapse >= perf_info.max_time)
 
  154    perf_info.max_time = lapse;
 
  155  else if (lapse <= perf_info.min_time)
 
  156    perf_info.min_time = lapse;
 
  159  perf_info.total_time += lapse;
 
  165  long double clock_end = (
long double)clock();
 
  176  long double lapse = clock_end - perf_info.
clock_start;
 
 
  186  map<string, PerformanceData>::iterator it;
 
 
  197         << std::setw(STOP_WATCH_MAX_NAME_LENGTH) << std::left
 
  198         << 
"*** PROFILING RESULTS [ms] ";
 
  199  output << std::setw(STOP_WATCH_TIME_WIDTH) << 
"min" 
  201  output << std::setw(STOP_WATCH_TIME_WIDTH) << 
"avg" 
  203  output << std::setw(STOP_WATCH_TIME_WIDTH) << 
"max" 
  205  output << std::setw(STOP_WATCH_TIME_WIDTH) << 
"lastTime" 
  207  output << std::setw(STOP_WATCH_TIME_WIDTH) << 
"nSamples" 
  209  output << std::setw(STOP_WATCH_TIME_WIDTH) << 
"totalTime" 
  211  map<string, PerformanceData>::iterator it;
 
  213    if (it->second.stops > 0) 
report(it->first, precision, output);
 
 
  236  std::cout << 
"Stopwatch active." << std::endl;
 
 
  241  std::cout << 
"Stopwatch inactive." << std::endl;
 
 
  246                       std::ostream& output) {
 
  255  output << std::setw(STOP_WATCH_MAX_NAME_LENGTH) << std::left << perf_name;
 
  256  output << std::fixed << std::setprecision(precision)
 
  257         << std::setw(STOP_WATCH_TIME_WIDTH) << (perf_info.
min_time * 1e3)
 
  259  output << std::fixed << std::setprecision(precision)
 
  260         << std::setw(STOP_WATCH_TIME_WIDTH)
 
  262  output << std::fixed << std::setprecision(precision)
 
  263         << std::setw(STOP_WATCH_TIME_WIDTH) << (perf_info.
max_time * 1e3)
 
  265  output << std::fixed << std::setprecision(precision)
 
  266         << std::setw(STOP_WATCH_TIME_WIDTH) << (perf_info.
last_time * 1e3)
 
  268  output << std::fixed << std::setprecision(precision)
 
  269         << std::setw(STOP_WATCH_TIME_WIDTH) << perf_info.
stops << 
" ";
 
  270  output << std::fixed << std::setprecision(precision)
 
  271         << std::setw(STOP_WATCH_TIME_WIDTH) << perf_info.
total_time * 1e3
 
 
  283  if (
mode == CPU_TIME) lapse /= (double)CLOCKS_PER_SEC;
 
 
  345  return Watcher(*
this, perf_name, perf_info);
 
 
  348void Stopwatch::Watcher::_start() {
 
  352    p = &w.get_or_create_perf(n);
 
  359void Stopwatch::Watcher::_stop() {
 
  360  if (!w.profiler_active || (p == 
nullptr)) 
return;
 
  362  long double clock_end = w.take_time();
 
  363  w.stop_perf(*p, clock_end);
 
bool active
Flag to hold the clock's status.
 
long double get_total_time(const std::string &perf_name)
Returns total execution time of a certain performance.
 
Stopwatch(StopwatchMode _mode=NONE)
Constructor.
 
void reset_all()
Resets all the performance records.
 
long double take_time()
Take time, depends on mode.
 
long double get_time_so_far(const std::string &perf_name)
Return the time since the start of the last measurement of a given performance.
 
void stop(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
 
void turn_on()
Turn on clock, restore clock operativity after a turn_off().
 
long double get_last_time(const std::string &perf_name)
Return last measurement of a certain performance.
 
std::map< std::string, PerformanceData > * records_of
Dynamic collection of performance data.
 
void start(const std::string &perf_name)
Start the stopwatch related to a certain piece of code.
 
void reset(const std::string &perf_name)
Reset a certain performance record.
 
Watcher watcher(const std::string &perf_name)
create a Start the stopwatch related to a certain piece of code
 
void disable_profiler()
Disable the profiler.
 
bool profiler_active
Indicates if the profiler is enabled.
 
void report_all(int precision=2, std::ostream &output=std::cout)
Dump the data of all the performance records.
 
long double get_max_time(const std::string &perf_name)
Returns maximum execution time of a certain performance.
 
long double get_average_time(const std::string &perf_name)
Returns average execution time of a certain performance.
 
void set_mode(StopwatchMode mode)
Initialize stopwatch to use a certain time taking mode.
 
long double get_min_time(const std::string &perf_name)
Returns minimum execution time of a certain performance.
 
void turn_off()
Turn off clock, all the Stopwatch::* methods return without doing anything after this method is calle...
 
bool performance_exists(std::string perf_name)
Tells if a performance with a certain ID exists.
 
void pause(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
 
void enable_profiler()
Enable the profiler.
 
StopwatchMode mode
Time taking mode.
 
void report(const std::string &perf_name, int precision=2, std::ostream &output=std::cout)
Dump the data of a certain performance record.