Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ clock()在调试版本上运行良好,但赢得了';t发布工作(C+;+;VS2010)_C++_Timer_Release_Clock - Fatal编程技术网

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:我怀疑任何编译器都不能很好地优化代码,使其能够返回时间!问题不仅仅是获得一个较小的时间值,而是得到一个负值。