C++ clock()在调试版本上运行良好,但赢得了';t发布工作(C+;+;VS2010)
这是我的代码:C++ clock()在调试版本上运行良好,但赢得了';t发布工作(C+;+;VS2010),c++,timer,release,clock,C++,Timer,Release,Clock,这是我的代码: // 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_PE
// 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;
//启动性能测试时钟
断言((开始=时钟())!=-1);
//一些阅读和写作方法
//停车时间
停止=时钟();
cout不应在assert
语句中指定start
<代码>断言
在发布版本中通常是不可操作的。因此,在调试版本中,将执行语句start=clock()
,但在发布版本中不会执行。因此,它可能未初始化(取决于早期代码和start
的声明)。通常需要避免使用有副作用的assert
语句;它可能导致调试版本和发布版本之间的细微差异/错误
最好是这样写:
start = clock();
assert(start != -1);
发布版本可能经过了大量优化,因此比调试版本花费的时间要少得多。通常,调试版本不使用优化。如果在发行版配置中禁用优化会发生什么情况(请确保在以后重新启用)?@WTP:我怀疑任何编译器都不能很好地优化代码,使其能够返回时间!问题不仅仅是获得一个较小的时间值,而是得到一个负值。