C/C++获取程序运行时间(纳秒级)

用到获取系统时间(纳秒)的函数getTimeNs()

包含头文件:ctime(c++)/time.h(c)

#include<ctime>
long getTimeNs()
{
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME,&ts);

    return ts.tv_sec*1000000000+ts.tv_nsec;
}	

只需要在需要测试时间的代码块前后用调用getTimeNs(),用两个变量接收返回值,相减就是纳秒级的运行时间。(●ˇ∀ˇ●)

posted @ 2020-05-27 23:17  cnwanglu  阅读(7150)  评论(0编辑  收藏  举报