linux c++性能测试函数

来源:互联网 发布:探测网络拓扑 编辑:程序博客网 时间:2024/06/06 18:33

测试函数的运行时间

#include<time.h>#include<sys/time.h>#include<stdio.h>// C++ 多线程编程总结// http://www.cnblogs.com/zhiranok/archive/2012/05/13/cpp_multi_thread.htmlstruct 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;}


0 0
原创粉丝点击