簡體   English   中英

clock()在調試版上運行良好但在發布時不起作用(C ++ VS2010)

[英]clock() works well on debug version but won't work on release (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更小的值,結果是負數。

有任何想法嗎?

start的賦值不應該在assert語句中。 assert通常是發布版本中的無操作。 因此在調試版本中,將執行語句start=clock() ,但在發布版本中,它不會。 因此,有可能(取決於早期的代碼和start聲明)它沒有被初始化。 通常希望避免使用具有副作用的assert語句; 它可能導致調試和發布版本之間的細微差異/錯誤。

寫它可能會更好:

start = clock();
assert(start != -1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM