![](/img/trans.png)
[英]How to measure instruction execution time (with Clock Cycle Unit) instead of second in C++?
[英]c++ implementing clock to measure execution time
我已经用C ++编写了一个程序,试图测量完全执行所花费的时间
int main (int argc, char**argv){
clock_t tStart = clock();
//doing my program's work here
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
return 0;
}
我的问题是,它将始终在执行时间内打印出0.00s。 可能是由于我的程序中使用了多个pthread(我的程序使用pthread_join来确保所有线程都已完成执行,所以我认为这不应该成为问题)吗?
编辑://正在做程序的工作= ...
for(i = 0;i<4;i++){
err = pthread_create(&threads[i], NULL, print, NULL);
pthread_join(threads[i], NULL);
}
void *print(void *data){
printf("hello world");
}
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
您的所有三个算术操作数都是整数,因此您执行整数除法并得到0
。
将/
符号的LHS或RHS强制转换为浮点类型。 并多次运行您的代码! 如果仅测量一次运行,则基准测试是无用的(这很明显,因为您得到0
,而不是1
或类似300
或类似值)。
它确实取决于//doing my program's work here
。 如果它正在启动其他线程,那么您肯定需要等待或轮询才能获得时间。 显示代码以获得更多帮助! 但是,在最近的类似情况下,事实证明我的代码实际上在不到0.01秒的时间内运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.