c++精准测试代码详细时间,并指定度量衡(纳秒、微秒、毫秒等)

auto start_ = std::chrono::high_resolution_clock::now();
/*
some code
*/
auto end_ = std::chrono::high_resolution_clock::now();
fprintf(stdin, %s last<%" PRIu64 ">\n", "some code ",                      
        std::chrono::duration_cast<std::chrono::nanoseconds>(              
       (end_ - start_time)).count());

将以上代码插入somecode,即可测量时间,其中可将nanoseconds替换来更换度量衡:

typedef duration <Rep, ratio<3600,1>> hours;
typedef duration <Rep, ratio<60,1>> minutes;
typedef duration <Rep, ratio<1,1>> seconds;
typedef duration <Rep, ratio<1,1000>> milliseconds;
typedef duration <Rep, ratio<1,1000000>> microseconds;
typedef duration <Rep, ratio<1,1000000000>> nanoseconds;

你可能感兴趣的:(c++学习日志)