Clock() 在调试版本上运行良好,但在发布版本上不起作用(C++ VS2010)
这是我的代码:
// 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 小得多,结果是负数。
有什么想法吗?
This is my code:
// 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;
It works great when I debug my program, but when I run the release version,
stop receives a much smaller value than start, and result is a negative number.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
start
的赋值不应出现在assert
语句中。assert
在发布版本中通常是无操作。因此,在调试版本中,将执行start=clock()
语句,但在发布版本中则不会执行。因此它可能没有初始化(取决于之前的代码和start
的声明)。通常希望避免使用有副作用的assert
语句;它可能会导致调试和发布版本之间出现细微的差异/错误。像这样写可能会更好:
The assignment of
start
should not be in theassert
statement.assert
is typically a no-op in release builds. So in the debug build, the statementstart=clock()
would be executed, but in the release build, it would not. So it is possible (depending on earlier code and the declaration ofstart
) that it is not initialized. It is typically desirable to avoid usingassert
statements that have side effects; it can lead to subtle differences/bugs between debug and release builds.It might be better to write it like this: