picard_cpp/src/common/utils/timer.cpp

89 lines
1.6 KiB
C++
Executable File

/*
Description: 用来统计程序段执行的时间,测试用
Copyright : All right reserved by ICT
Author : Zhang Zhonghai
Date : 2019/01/30
*/
#include "timer.h"
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
/*
* class Timer implementation
*/
double Timer::mseconds_elapsed() {
return (get_mseconds() -start_);
}
double Timer::seconds_elapsed() {
return (mseconds_elapsed() / 1000);
}
void Timer::reinit() {
start_ = get_mseconds();
}
void Timer::reinit_acc() {
acc_count_ = 0.0;
}
void Timer::acc_start() {
acc_start_ = get_mseconds();
}
void Timer::acc_end() {
acc_count_ += get_mseconds() - acc_start_;
}
double Timer::acc_mseconds_elapsed() {
return acc_count_;
}
double Timer::acc_seconds_elapsed() {
return acc_count_ / 1000;
}
void Timer::log_time(const char *desc) {
printf("[%s] ", desc);
print_current_time();
}
void Timer::print_current_time() {
time_t time_val;
struct tm *at;
char now[80];
time(&time_val);
at = localtime(&time_val);
strftime(now, 79, "%Y-%m-%d %H:%M:%S", at);
puts(now);
}
string Timer::get_current_time_str() {
time_t time_val;
struct tm *at;
char now[80];
time(&time_val);
at = localtime(&time_val);
strftime(now, 79, "%B %d, %Y at %I:%M:%S %p %Z", at);
return string(now);
}
double Timer::get_mseconds() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double) 1000 * (tv.tv_sec + ((1e-6) * tv.tv_usec));
}
double Timer::get_seconds() {
return 1000 * get_mseconds();
}
/********** end of Timer implementation *************/