在运行c++程序,需要记录每个函数的运行时间开销,传统采用clock记录比较麻烦。发现一个比较好的工具:
http://valgrind.org/docs/manual/cl-manual.html, 编译好程序, 可以使用如下命令:
valgrind --tool=callgrind [callgrindoptions] your-program [program options] 跑程序,然后采用KCachegrind(http://kcachegrind.sourceforge.net/html/Home.html) 工具视觉化具体参数。
具体界面说明参见:
http://baptiste-wicht.com/posts/2011/09/profile-c-application-with-callgrind-kcachegrind.html
该工具的好处是可以一目了然看出那个函数的开销是比较多,进一步进行时间优化。比如我跑的一个程序的strtol函数开销大,通过其他方式可以避免使用atoi,atol等函数。