#include <ctime>

 

毫秒

// clock_t是一个长整形数。
// 在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
// #define CLOCKS_PER_SEC ((clock_t)1000) 
// 每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。

srand(time(NULL)); //设置随机数种子 clock_t startTime,endTime; startTime = clock();//计时开始 // func(); endTime = clock();//计时结束 printf("The run time is: %lf s\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);

 

纳秒

/* 获取纳秒时间 */
long long GetTimeNano(){
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    return ts.tv_sec * 1000000000 + ts.tv_nsec;
}

long long startTime = GetTimeNano();
// func();
long long endTime = GetTimeNano();
printf("%.8lf ms\n", (double)(endTime - startTime) / 1000000);

 

相关文章:

  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2021-08-21
  • 2022-12-23
  • 2021-10-29
  • 2021-09-22
  • 2022-12-23
相关资源
相似解决方案