#include
#include
#include
#include
int foo(int i)
{
return i;
}
const int64_t MAX_COUNT = 100000*10000;
struct TimerEval {
TimerEval(const char* module)
{
start_time_ = time(NULL);
module_ = module;
}
~TimerEval()
{
time_t end_time = time(NULL);
printf("%s elapse : %ld sec\n", module_, (end_time - start_time_));
}
time_t start_time_;
const char* module_;
};
int main()
{
struct timeval tpTmp;
printf("repeat %ld times, test result is : \n", MAX_COUNT);
{
TimerEval eval("call fun");
for (int i=0; i
}
{
TimerEval eval("call time");
for (int i=0; i
}
{
TimerEval eval("call gettimeofday");
for (int i=0; i
}
{
TimerEval eval("call clock_gettime");
struct timespec tp;
for (int i=0; i
}
return 0;
}
两次测试结果为(MAX_COUNT不同):
repeat 100000000 times, test result is :
call fun elapse : 0 sec
call time elapse : 0 sec
call gettimeofday elapse : 1 sec
call clock_gettime elapse : 2 sec
repeat 1000000000 times, test result is :
call fun elapse : 1 sec
call time elapse : 2 sec
call gettimeofday elapse : 13 sec
call clock_gettime elapse : 13 sec