我试图找到一种方法来获取C中一段代码的执行时间。我已经尝试过time.h中的time()和clock(),但是time()似乎返回秒,而clock()似乎给我毫秒(或厘米秒?)不过,我想要更精确的东西。有没有一种方法可以让我的时间精度至少达到微秒?
这只需要能够在Linux上编译即可。
发布于 2008-10-06 07:15:13
你提到了clock()和time() --你是在找gettimeofday()吗?这将填充一个包含秒和微秒的struct timeval。
当然,实际的分辨率取决于硬件。
发布于 2008-10-06 16:33:35
对于它的价值,这里有一个只是一些宏:
#include <time.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define PRINTTIME printf( "%6.3f seconds used by the processor.", ((double)stopm-startm)/CLOCKS_PER_SEC);然后只需将其与以下命令一起使用:
main() {
START;
// Do stuff you want to time
STOP;
PRINTTIME;
}来自http://ctips.pbwiki.com/Timer
发布于 2008-10-06 07:08:25
您需要一个profiler应用程序。
SO和搜索引擎中的搜索关键字: linux分析
https://stackoverflow.com/questions/173409
复制相似问题