博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux精确分析函数运行时间
阅读量:4179 次
发布时间:2019-05-26

本文共 968 字,大约阅读时间需要 3 分钟。

网上大部分用clock函数,在线程中计算某个函数运行时间,很不精确

#ifndef PROFILER_H#define PROFILER_H#include 
#include
#include
#include
class profiler{public: 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); printf("[%s]cost=%d ms\n", m_func_name, cost / 1000); }private: struct timeval tv; const char * m_func_name;};#define PROFILER() profiler ____profiler_instance##__LINE__(__FUNCTION__)#endif // PROFILER_H

 

 

 

不精确的做法:

static clock_t m_cockStart;void closkStart(){    m_cockStart = clock();}void clockFinish(const char* msg){    clock_t finish;    double total_time;    finish = clock();    total_time = (double)(finish-m_cockStart) / CLOCKS_PER_SEC;    printf( "%s:%f seconds\n", msg, total_time);}

 

 

转载地址:http://fzmai.baihongyu.com/

你可能感兴趣的文章
json, recyclerView问题
查看>>
cmake处理多源文件目录的方法
查看>>
Service Intent must be explicit
查看>>
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
德鲁伊druid 数据源配置
查看>>
NO message found under code 'xxxxx' locale 'zh_CN'
查看>>
Jenkins-部署(一)
查看>>
Jenkins-配置邮箱
查看>>
六、同一个tomcat多个web应用共享session
查看>>
Tomcat配置注意事项
查看>>
Tomcat下面的配置在每次Eclipse编译后,修改好的配置会还原
查看>>
Eclipse代码格式模板导入
查看>>
list与Set、Map区别及适用场景
查看>>
Jenkins自动部署Maven 多个子项目
查看>>
Oracle连接DB2
查看>>
CXF配置
查看>>
WSDL文件解析
查看>>
MyEclipse安装Flash Builder插件
查看>>