/*
* 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 NPERSEC 1000000000
#ifdef __cplusplus
extern "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 __cplusplus
class 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="https://img-blog.csdn.net/20141106150547248" alt="" />
<img src="https://img-blog.csdn.net/20141106150705078" alt="" />
<img src="https://img-blog.csdn.net/20141106150621834" alt="" />
<img src="https://img-blog.csdn.net/20141106150712625" alt="" />
<img src="https://img-blog.csdn.net/20141106150447406" alt="" />
#endif //__cplusplus
#endif //__WQ_PERF__