测量时间结果返回值为0或0.001 [英] Measuring time results in return values of 0 or 0.001
问题描述
我试图使用 chrono :: steady_clock
来测量程序中代码块之间的小数秒。我有这个代码块在LiveWorkSpace工作( http://liveworkspace.org/code/YT1I$9):
#include< chrono>
#include< iostream>
#include< vector>
int main()
{
auto start = std :: chrono :: steady_clock :: now();
for(unsigned long long int i = 0; i <10000; ++ i){
std :: vector< int> v(i,1);
}
auto end = std :: chrono :: steady_clock :: now();
auto difference = std :: chrono :: duration_cast< std :: chrono :: microseconds>(end-start).count();
std :: cout<< 自开始后的秒数:< ((双)差/ 1000000);当我在我的程序中实现相同的想法,像这样:
$ b <
auto start = std :: chrono :: steady_clock :: now();
//代码块到时间
auto end = std :: chrono :: stead_clock :: now();
auto difference = std :: chrono :: duration_cast< std :: chrono :: microseconds>(end-start).count()
std :: cout< < 自开始后的秒数:< ((双)差/ 1000000);
程序只打印出 0
和 0.001
。我非常怀疑我的代码块的执行时间总是等于 0
或 1000
微秒,这个舍入和如何消除它,以便我可以得到正确的小数值?
这是一个Windows程序。
解决方案在MSVC2012上运行一些测试后,我可以确认Microsoft的实现中的C ++ 11时钟没有足够高的分辨率。有关此问题的错误报告,请参见 C ++标题的high_resolution_clock不具有高分辨率 。
因此,不幸的是,对于更高分辨率的计时器,您需要使用 boost :: chrono
或QueryPerformanceCounter像这样,直到他们修复错误:
#include< iostream>
#include< Windows.h>
int main()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(& frequency);
LARGE_INTEGER start;
QueryPerformanceCounter(& start);
//将代码放入时间
LARGE_INTEGER end;
QueryPerformanceCounter(& end);
//微秒使用1000000.0
double interval = static_cast< double>(end.QuadPart- start.QuadPart)/
frequency.QuadPart; // in seconds
std :: cout<<间隔;
}
I am trying to use chrono::steady_clock
to measure fractional seconds elapsed between a block of code in my program. I have this block of code working in LiveWorkSpace (http://liveworkspace.org/code/YT1I$9):
#include <chrono>
#include <iostream>
#include <vector>
int main()
{
auto start = std::chrono::steady_clock::now();
for (unsigned long long int i = 0; i < 10000; ++i) {
std::vector<int> v(i, 1);
}
auto end = std::chrono::steady_clock::now();
auto difference = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "seconds since start: " << ((double)difference / 1000000);
}
When I implement the same idea into my program like so:
auto start = std::chrono::steady_clock::now();
// block of code to time
auto end = std::chrono::stead_clock::now();
auto difference = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()
std::cout << "seconds since start: " << ((double) difference / 1000000);
The program will only print out values of 0
and 0.001
. I highly doubt that the execution time for my block of code always equals 0
or 1000
microseconds, so what is accounting for this rounding and how might I eliminate it so that I can get the proper fractional values?
This is a Windows program.
解决方案 After running some tests on MSVC2012, I could confirm that the C++11 clocks in Microsoft's implementation do not have a high enough resolution. See C++ header's high_resolution_clock does not have high resolution for a bug report concerning this issue.
So, unfortunately for a higher resolution timer, you will need to use boost::chrono
or QueryPerformanceCounter directly like so until they fix the bug:
#include <iostream>
#include <Windows.h>
int main()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
LARGE_INTEGER start;
QueryPerformanceCounter(&start);
// Put code here to time
LARGE_INTEGER end;
QueryPerformanceCounter(&end);
// for microseconds use 1000000.0
double interval = static_cast<double>(end.QuadPart- start.QuadPart) /
frequency.QuadPart; // in seconds
std::cout << interval;
}
这篇关于测量时间结果返回值为0或0.001的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!