精确记算程序的运行时间或者某段代码的运行时间
来源:互联网 发布:unity3d 动态生成模型 编辑:程序博客网 时间:2024/05/17 04:10
程序的运行效率很重要,为了明确到底是那一块代码浪费时间,浪费多少时间,检测一下是很有必要的,用下面的方法可以精确地统计时间。第一种精确到秒,第二种精确到毫秒,第三种精确到0.000001秒,大家可以根据自己的需求选用。
#include<time.h>#include<stdio.h>#include<stdlib.h>#include<windows.h>int main(){ //精确到秒 ========================================== time_t t1,t2; time(&t1); //此处放置要测试的代码 Sleep(1000);//延时 time(&t2); printf("%d %d %d秒\n",t1,t2,t2-t1); //精确到毫秒 ======================================== clock_t c1,c2; c1=clock(); //此处放置要测试的代码 Sleep(100);//延时 c2=clock(); printf("%d %d %d毫秒\n",c1,c2,c2-c1); //精确到 0.000001毫秒 =============================== LARGE_INTEGER litmp; LONGLONG start, end; double dft, dff, dfm; QueryPerformanceFrequency(&litmp);//获得时钟频率 dff = (double) litmp.QuadPart; QueryPerformanceCounter(&litmp);//获得初始值 start = litmp.QuadPart; //此处放置要测试的代码 Sleep(1000);//延时 QueryPerformanceCounter(&litmp);//获得终止值 end = litmp.QuadPart; dfm = (double) (end - start); dft = dfm / dff;//获得对应的时间值,单位秒 printf("%lf毫秒\n",dfm/dff*1000);}
那是在windows下的,如果在linux环境下,要这样计算时间:
struct timeval tv_start,tv_end;gettimeofday(&tv_start,NULL);//此处放要测试的代码或程序gettimeofday(&tv_end,NULL);printf("程序运行时间为:%lf秒\n",tv_end.tv_sec-tv_start.tv_sec+(tv_end.tv_usec-tv_start.tv_usec)/1000000.0); //其中timeval的结构题定义是这样的:struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds 微秒=1/10^6秒 */};
tv_sec和tv_usec可以单独输出,根据自己的需求选择精度就可以了
0 0
- 精确记算程序的运行时间或者某段代码的运行时间
- 精确记算程序的运行时间或者某段代码的运行时间
- 精确记算程序的运行时间或者某段代码的运行时间
- .net代码段运行时间的精确测试
- 计算某段代码的运行时间
- 精确计算程序运行的时间
- 计算程序运行的某一段时间
- ios代码段运行时间的测定
- 计算代码段的运行时间
- Linux学习2之shell脚本计算代码段运行的时间(精确到毫秒)
- C++记录某段程序的运行时间
- 如何得知某段代码的运行时间
- C语言-计算某段代码的运行时间
- 记录某段代码运行时间的函数C++
- C语言-计算某段代码的运行时间
- 精确统计程序运行时间
- 精确测量程序运行时间
- 精确计算代码运行时间
- Swift - 动画效果的实现方法总结
- 使用过滤器格式化数据
- 打JAR包 路径不是根目录的问题
- 数据结构-2
- session共享
- 精确记算程序的运行时间或者某段代码的运行时间
- ARC中_autoreleasing
- 项目----1(在项目开发中AsyncTask与Activity生命周期不一导致内存泄露的问题)
- 如何实现一个malloc
- panel的content属性
- 【CSS3】结构性伪类选择器—not
- xcode 导入七牛 objc-sdk
- Caused by: java.lang.ClassCastException: org.hibernate.type.StringType cannot be cast to org.hiberna
- 前端开发