时延打点
1、chrono的std::chrono::high_resolution_clock::now() 可以到纳秒
2、gettimeofday() 到微秒us
/* Get the current time of day and timezone information,
putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.
Returns 0 on success, -1 on errors.
NOTE: This form of timezone information is obsolete.
Use the functions and variables declared in <time.h> instead. */
extern int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz) __THROW __nonnull ((1));
使用
#include "../3th/include/benchmark/benchmark.h"
#include<chrono>
#include <sys/time.h>
using namespace std;
using namespace chrono;
static void recordTimeByChrono(benchmark::State& state)
{
typedef std::chrono::high_resolution_clock Clock;
for (auto _ : state)
{
auto t1 = Clock::now();//计时开始
// do something
auto t2 = Clock::now();//计时结束
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
// std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
}
}
BENCHMARK(recordTimeByChrono)->Iterations(100000);
BENCHMARK(recordTimeByChrono)->Iterations(10000);
BENCHMARK(recordTimeByChrono)->Iterations(1000);
BENCHMARK(recordTimeByChrono)->Iterations(100);
static void recordTimeByGettimeofday(benchmark::State& state)
{
for (auto _ : state)
{
struct timeval t1;
struct timeval t2;
gettimeofday(&t1,NULL);
// do something
gettimeofday(&t2,NULL);
(t2.tv_sec - t1.tv_sec)*1000000 + t2.tv_usec-t1.tv_usec;
}
}
BENCHMARK(recordTimeByGettimeofday)->Iterations(100000);
BENCHMARK(recordTimeByGettimeofday)->Iterations(10000);
BENCHMARK(recordTimeByGettimeofday)->Iterations(1000);
BENCHMARK(recordTimeByGettimeofday)->Iterations(100);