测试函数的运行时间
#include<time.h>
#include<sys/time.h>
#include<stdio.h>
// C++ 多线程编程总结
// http://www.cnblogs.com/zhiranok/archive/2012/05/13/cpp_multi_thread.html
struct profiler{
profiler(const char* func_name){
gettimeofday(&tv, NULL);
m_func_name=func_name;
}
~profiler(){
struct timeval tv2;
gettimeofday(&tv2, NULL);
long cost = (tv2.tv_sec - tv.tv_sec) * 1000000 + (tv2.tv_usec - tv.tv_usec);
//! post to some manager
//test:
printf("[%s]cost=%d ms\n",m_func_name,cost);
}
struct timeval tv;
const char * m_func_name;
};
#define PROFILER() profiler ____profiler_instance##__LINE__(__FUNCTION__)
void func(){
PROFILER();
for(int i=0;i<1000;i++);
printf("\n\nfunction is end!\n\n");
}
int main(){
func();
return 0;
}