C ++ chrono :: duration_cast始终输出"0秒",
这是我的第一个问题,我也是C ++的新手,但我会尽力做到尽可能具体.请告诉我是否要模糊:
This is my very first question here and I'm also a complete newbie at C++, but I'll do my best to be as specific as possible. Please tell me if I'm being to vague:
我正在尝试测量使用chrono和duration_cast进行排序的方法(合并排序)对给定的整数数组进行排序所花费的时间.这是有问题的代码段:
I am trying to measure the time it takes for a sorting method (merge sort) to sort a given array of integers by using chrono and duration_cast. Here is the code snippet in question:
auto t1 = std::chrono::high_resolution_clock::now();
mergesort(sortingArray, temp, 0, num - 1);
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> fp_ms = t2 - t1;
std::cout << fp_ms.count() << " seconds\n";
无论我将它需要排序的数组多大,我得到的输出始终为"0秒".即使对一百万个整数进行排序并且有明显的执行时间,它仍然可以提供相同的输出.
And the output I get is always "0 seconds", no matter how big I make the array it needs to sort. Even when it sorts a million integers and there is a noticeable execution time, it still gives me the same output.
我基本上是按照此处给出的示例进行操作: http://en. cppreference.com/w/cpp/chrono/duration/duration_cast
I'm basically following the example given here: http://en.cppreference.com/w/cpp/chrono/duration/duration_cast
我正在使用我的mergesort函数而不是f().如何使它正确地衡量我的排序方法?
Only instead of f() I'm using my mergesort function. How can I make it measure my sorting method properly?
我正在使用minGW在Windows 10中通过Powershell进行编译.该命令如下所示:
I'm using minGW to compile via Powershell in Windows 10. The command looks like this:
g++ -std=c++11 .\Merge.cpp
TL; DR :在Windows上,std::chrono
实现(libstdc ++)看起来很差,您将无法获得比秒更好的东西.
TL;DR: It looks like the std::chrono
implementation (libstdc++) is quite poor on Windows and you won't get anything better than seconds.
长版:
libstdc ++ typedef
s std::chrono::high_resolution_clock
至std::chrono::system_clock
.根据
libstdc++ typedef
s std::chrono::high_resolution_clock
to std::chrono::system_clock
. According to the implementation a call to std::chrono::system_clock::now()
will result in a call to one of the following, depending on the platform:
-
syscall(SYS_clock_gettime, CLOCK_REALTIME, ...)
,这是Linux系统调用 -
clock_gettime(CLOCK_REALTIME, ...)
,这是Windows不支持的POSIX系统调用 -
gettimeofday(...)
,这是Windows不支持的POSIX函数 -
std::time()
作为后备广告
-
syscall(SYS_clock_gettime, CLOCK_REALTIME, ...)
, which is a Linux system call -
clock_gettime(CLOCK_REALTIME, ...)
, which is a POSIX syscall not supported by Windows -
gettimeofday(...)
, which is a POSIX function not supported by Windows -
std::time()
as a fallback
因此,std::time()
在Windows内部被调用.未指定std::time()
的编码.但是,大多数系统遵循 POSIX规范:
Thus, std::time()
is called internally on Windows. The encoding of std::time()
is not specified; however, most systems follow the POSIX specification:
time()函数应返回自新纪元以来的时间值,以秒为单位.
Microsoft本身相同:
Microsoft itself does the same:
返回自1970年1月1日午夜起经过的时间(
seconds ),如果发生错误,则返回-1.
Return the time as seconds elapsed since midnight, January 1, 1970, or -1 in the case of an error.
我想可以肯定地说,MingW的std::chrono
不会使您获得更高的分辨率.
I think it is safe to say that you won't get a higher resolution with MingW's std::chrono
.
对于您的问题,您有两种选择:
As for your problem, you have two options:
- 如果您的程序仅在Windows上运行,则可以使用 Boost.Chrono .它使用本地Windows API ,并且应该提供更好的分辨率.
- If your program is running on Windows only you can build your own time measurement using QueryPerformanceCounter
- If you want to keep it portable use Boost.Chrono. It uses the native Windows APIs and should offer a better resolution.