【发布时间】:2011-12-22 08:45:50
【问题描述】:
这是我的代码:
// Start performance test clock
assert((start=clock())!=-1);
// Some reading and writing methods
// Get stop time
stop = clock();
cout << stop << endl;
// Calculate operation time
double result = (double)(stop-start)/CLOCKS_PER_SEC;
// Print result
cout << "--> Finished analysing in " << result << "s" << endl;
当我调试我的程序时效果很好,但是当我运行发布版本时, stop 接收的值比 start 小得多,结果是负数。
有什么想法吗?
【问题讨论】:
-
发行版可能会进行大量优化,因此比调试版花费的时间要少得多。通常,调试版本不使用优化。如果您在发布配置中禁用优化会发生什么(确保之后重新启用它)?
-
@WTP:我怀疑任何编译器都可以很好地优化代码,以至于它可以及时返回!问题不仅在于获得较小的时间值,还在于获得负值。
-
旁白:考虑使用Boost timer library。