用于C/C++统计时间
来源:互联网 发布:我好后悔网络棋牌赌博 编辑:程序博客网 时间:2024/05/18 01:18
/* * DESCRIPTION: * * WQ_PERF_*** must be used in pairs. Example: WQ_PERF_BEGIN()... ... WQ_PERF_END() * * WQ_PERF_BEGIN() and WQ_PERF_END() is used calculate the time between them. * * WQ_PERF_LOOP_BEGIN(count, type) WQ_PERF_LOOP_END() * ---count is the REPEAT COUNTS to executive the program between them; * ---type is the INDICATOR whether print the each repeat result, type == 1, don't; type != 1 print. */#ifndef __WQ_PERF__#define __WQ_PERF__#include <stdio.h>#include <time.h>#define NPERSEC1000000000#ifdef __cplusplusextern "C" {#endif#define WQ_PERF_BEGIN()\{\struct timespec __begin__, __end__;\int __line__ = __LINE__;\clock_gettime(CLOCK_MONOTONIC, &__begin__);#define WQ_PERF_END()clock_gettime(CLOCK_MONOTONIC, &__end__);\int __flag__ = __end__.tv_nsec < __begin__.tv_nsec; \int __sec__ = __end__.tv_sec - __begin__.tv_sec - __flag__;\long long __nsec__ = __end__.tv_nsec + __flag__*NPERSEC -__begin__.tv_nsec;\printf("\n------------------BEGIN %s: LINE: %d~%d -----------------------\n", __FILE__, __line__,__LINE__);\printf("consuming time:(%fs)\t", __sec__ + (double)__nsec__/NPERSEC);\if (__sec__ != 0)\printf("%2ds, ", __sec__);\printf("%9dns\n", __nsec__);\printf("------------------END %s: LINE: %d~%d -----------------------\n", __FILE__, __line__, __LINE__);\} #define WQ_PERF_LOOP_BEGIN(count, type)\{\int __line__ = __LINE__;\int __count__ = (count);\int __type__ = (type);\long long __total__ = 0;\int __i__, __min__, __max__;\double __vari__;\double __arver__ = 0;\long *__array__ = NULL;\if ((__count__) <= 0)goto error;\struct timespec __begin__, __end__; \__array__ = (long *)malloc(sizeof(long) * __count__);\if (__array__ == NULL)goto error;\__min__ = __max__ = 0;\for (__i__ = 0; __i__ < __count__; __i__++) {\clock_gettime(CLOCK_MONOTONIC, &__begin__);\#define WQ_PERF_LOOP_END()\clock_gettime(CLOCK_MONOTONIC, &__end__);\int __flag__ = __end__.tv_nsec < __begin__.tv_nsec; \long __temp__ = (__end__.tv_sec - __begin__.tv_sec - __flag__)*NPERSEC + \__end__.tv_nsec + __flag__*NPERSEC -__begin__.tv_nsec;\__total__ += __temp__;\__array__[__i__] = __temp__;\if (__temp__ < __array__[__min__])__min__ = __i__;\if (__temp__ > __array__[__max__])__max__ = __i__;\}\printf("\n----------------LOOP %d BEGIN %s: LINE: %d~%d ----------------------\n", __count__, __FILE__, __line__, __LINE__);\printf("repeat %3d consuming time:(%fs)\t%2ds,%9dns\n", __count__,\(double)__total__/NPERSEC, __total__/NPERSEC, __total__%NPERSEC);\printf("average consuming time:(%fs)\t%2ds,%9dns\n",(double)(__total__/__count__)/NPERSEC,\__total__/__count__/NPERSEC, __total__/__count__%NPERSEC);\printf("min consuming time:(%fs)\t%2ds,%9dns\n",(double)__array__[__min__]/NPERSEC,\__array__[__min__]/NPERSEC, __array__[__min__]%NPERSEC);\printf("max consuming time:(%fs)\t%2ds,%9dns\n",(double)__array__[__max__]/NPERSEC,\__array__[__max__]/NPERSEC, __array__[__max__]%NPERSEC);\printf("----------------------------------------------------------\n");\ __arver__ = __total__/__count__;\ __vari__ = 0;\for (__i__ = 0; __i__ < __count__; __i__++) {\if (__type__ != 0 )\printf("%d\t consuming time:(%fs)\t%2ds,%9dns\n",__i__,(double)__array__[__i__]/NPERSEC,\__array__[__i__]/NPERSEC, __array__[__i__]%NPERSEC);\__vari__ += ((__array__[__i__]-__arver__)/1000)* ((__array__[__i__]-__arver__)/1000);\}\printf("variance is %f (us)\n", __vari__/__count__);\printf("variance is %f (ms)\n", __vari__/__count__/1000000);\printf("variance is %f ( s)\n", __vari__/__count__/1000000000000);\free(__array__);\printf("----------------LOOP %d END %s: LINE: %d~%d ----------------------\n", __count__, __FILE__, __line__, __LINE__);\error:\;\}#ifdef __cplusplus}#endif //__cpusplus#ifdef __cplusplusclass wq_perf {public:wq_perf(int dis = 1) : display(dis) {if (display == 2)printf("\n---------------------------performance info---------------------------------\n");clock_gettime(CLOCK_MONOTONIC, &begin);}~wq_perf() {if (display <= 1)clock_gettime(CLOCK_MONOTONIC, &end);if (display > 0) {int flag = end.tv_nsec < begin.tv_nsec;int sec = end.tv_sec - begin.tv_sec - flag;long long nsec = end.tv_nsec + flag*NPERSEC -begin.tv_nsec;if (display == 1) printf("\n-------------------performance info--------------------------\n");if (display >= 2)printf("%3d\t", display-2);printf("consuming time:(%fs)\t", sec + (double)nsec/NPERSEC);if (sec != 0)printf("%2ds, ", sec);printf("%9dns\n", nsec);if (display == 1) printf("-----------------performance info end-----------------------\n");}}inline long gettime() {clock_gettime(CLOCK_MONOTONIC, &end);int flag = end.tv_nsec < begin.tv_nsec;return (end.tv_sec - begin.tv_sec -flag)*NPERSEC + end.tv_nsec + flag * NPERSEC - begin.tv_nsec;}private:struct timespec begin, end;int display;};#define WQ_PERF_CLASS_BEGIN(count,type)\{\int __line__ = __LINE__;\int __count__ = (count);\int __type__ = (type);\long long __total__ = 0;\long *__array__ = NULL;\int __i__, __min__, __max__;\double __vari__ = 0;\double __arver__ =0;\if ((__count__) <= 0)goto error;\struct timespec __begin__, __end__; \__array__ = (long *)malloc(sizeof(long) * __count__);\if (__array__ == NULL)goto error;\__min__ = __max__ = 0;\for (__i__ = 0; __i__ < __count__; __i__++) {\wq_perf __temp__(__type__ > 0 ? __i__+2:0);\#define WQ_PERF_CLASS_END()\__array__[__i__] = __temp__.gettime();\__total__ += __array__[__i__];\if (__array__[__i__] < __array__[__min__]) __min__ = __i__;\if (__array__[__i__] > __array__[__max__]) __max__ = __i__;\}\if (__type__ == 0)printf("\n");\printf("----------------REPEAT %d BEGIN %s: LINE: %d~%d ----------------------\n", __count__, __FILE__, __line__, __LINE__);\printf("repeat %3d consuming time:(%fs)\t%2ds,%9dns\n", __count__,\(double)__total__/NPERSEC, __total__/NPERSEC, __total__%NPERSEC);\printf("average consuming time:(%fs)\t%2ds,%9dns\n",(double)(__total__/__count__)/NPERSEC,\__total__/__count__/NPERSEC, __total__/__count__%NPERSEC);\printf("min consuming time:(%fs)\t%2ds,%9dns\n",(double)__array__[__min__]/NPERSEC,\__array__[__min__]/NPERSEC, __array__[__min__]%NPERSEC);\printf("max consuming time:(%fs)\t%2ds,%9dns\n",(double)__array__[__max__]/NPERSEC,\__array__[__max__]/NPERSEC, __array__[__max__]%NPERSEC);\__arver__ = __total__/__count__;\for (__i__ = 0; __i__ < __count__; __i__++) {\__vari__ += ((__array__[__i__]-__arver__)/1000)* ((__array__[__i__]-__arver__)/1000);\}\printf("variance is %f (us)\n", __vari__/__count__);\printf("variance is %f (ms)\n", __vari__/__count__/1000000);\printf("variance is %f ( s)\n", __vari__/__count__/1000000000000);\free(__array__);\printf("----------------REPEAT %d END %s: LINE: %d~%d ----------------------\n", __count__, __FILE__, __line__, __LINE__);\error:\;\}<img src="http://img.blog.csdn.net/20141106150547248" alt="" /><img src="http://img.blog.csdn.net/20141106150705078" alt="" />
<img src="http://img.blog.csdn.net/20141106150621834" alt="" />
<img src="http://img.blog.csdn.net/20141106150712625" alt="" />
<img src="http://img.blog.csdn.net/20141106150447406" alt="" />#endif //__cplusplus#endif //__WQ_PERF__
0 0
- 用于C/C++统计时间
- c统计时间
- C++Qt 统计运行时间
- C/C++/Qt 统计运行时间
- C/C++/Qt 统计运行时间
- C/C++ 统计函数调用时间
- C/C++/Qt软件系统统计运行时间方法详解
- 二分查找---C语言(统计了运行时间)
- 【C++】利用析构统计函数运行时间
- 一个用于统计输入中各个C语言关键字出现次数的程序
- 字符个数统计(C/C++)
- 统计日访问量(c#)
- 用C统计单词
- 【C】单词统计
- C统计单词个数
- Problem C: 统计成绩
- C-统计个位数
- c语言 词频统计
- xcode shortcuts
- Delphi Setlength 内存释放总结
- 高大上的几种交换两个数
- java里面的构造方法
- 关于Spring MVC的详细步骤(包括web.xml;applicationContext.xml;dispatcher-servlet.xml)
- 用于C/C++统计时间
- 数据挖掘之关联规则
- Linux下的real UID和effective UID
- x86, x86_64, AMD64, x64,386,586,686这些是什么意思
- Hbase 查询过程详解(基于hbase0.98版本后分析的)
- Delphi下dll的创建与使用
- 上传图片到服务器的实现
- myeclipse10注册机详细使用方法
- Android--监听EditText文本输入