简体   繁体   English

获得时间c ++

[英]Obtain time c++

I am trying to convert a Windows c++ function to a portable one. 我试图将Windows c ++函数转换为可移植的函数。 The objective of the function is to obtain a reference cpu time in seconds. 该函数的目标是以秒为单位获取引用cpu时间。 The Windows function uses QueryPerformanceCounter and QueryPerformanceFrequency , which are not portable to mac, so I have tried to use std::chrono::high_resolution_clock , but I do not understand how it works. Windows函数使用QueryPerformanceCounterQueryPerformanceFrequency ,它们不能移植到mac,因此我尝试使用std::chrono::high_resolution_clock ,但我不明白它是如何工作的。

The function that I already have is the following: 我已经拥有的功能如下:

double GetSeconds(void)
{
  double sec;
  LARGE_INTEGER Frequency, PerformanceCount;

  QueryPerformanceFrequency( &Frequency );
  QueryPerformanceCounter( &PerformanceCount );

  sec = (double)PerformanceCount.QuadPart /(double)Frequency.QuadPart;

  return(sec);
}

With high_resolution_clock I have the following code working (it returns the time that printing "Hello World" takes): 使用high_resolution_clock我有以下代码工作(它返回打印“Hello World”所需的时间):

std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::cout << "Hello World\n";
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
float a = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();

But I do not understand why the following line does not work (I just want to obtain the start time point): 但我不明白为什么以下行不起作用(我只想获取开始时间点):

float a = std::chrono::duration_cast<std::chrono::seconds>(start).count();    

Thank you for your help 谢谢您的帮助

Because start isn't a duration , it's a time point . 因为start不是持续时间 ,所以这是一个时间点 Also note that std::chrono::steady_clock is not related to the wall clock, so getting its current value in itself doesn't really tell you that much, it's just a counter that steadily counts up. 另请注意, std::chrono::steady_clock与挂钟无关,因此获取其当前值并不能真正告诉您那么多,它只是一个稳定计数的计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM