C++ 如何在C++中获取系统时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/599321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get system time in C++?
提问by Mobin
In fact i am trying to calculate the time a function takes to complete in my program. So i am using the logic to get system time when i call the function and time when the function returns a value then by subtracting the values i get time it took to complete. So if anyone can tell me some better approach or just how to get system time at an instance it would be quite a help
事实上,我正在尝试计算一个函数在我的程序中完成所需的时间。所以我使用逻辑来获取系统时间,当我调用函数和函数返回值时的时间,然后通过减去我得到完成时间的值。因此,如果有人能告诉我一些更好的方法或如何在某个实例中获取系统时间,那将非常有帮助
回答by John T
The approach I use when timing my code is the time() function. It returns a single numeric value to you representing the epoch which makes the subtraction part easier for calculation.
我在为代码计时时使用的方法是 time() 函数。它向您返回一个代表时代的单个数值,这使得减法部分更容易计算。
Relevant code:
相关代码:
#include <time.h>
#include <iostream>
int main (int argc, char *argv[]) {
int startTime, endTime, totalTime;
startTime = time(NULL);
/* relevant code to benchmark in here */
endTime = time(NULL);
totalTime = endTime - startTime;
std::cout << "Runtime: " << totalTime << " seconds.";
return 0;
}
Keep in mind this is user time. For CPU, time see Ben's reply.
请记住,这是用户时间。对于 CPU,时间见 Ben 的回复。
回答by Grant Peters
Your question is totally dependant on WHICH system you are using. Each system has its own functions for getting the current time. For finding out how long the system has been running, you'd want to access one of the "high resolution performance counters". If you don't use a performance counter, you are usually limited to microsecond accuracy (or worse) which is almost useless in profiling the speed of a function.
您的问题完全取决于您使用的是哪个系统。每个系统都有自己的获取当前时间的功能。要了解系统运行了多长时间,您需要访问“高分辨率性能计数器”之一。如果您不使用性能计数器,则通常仅限于微秒精度(或更差),这对于分析函数的速度几乎毫无用处。
In Windows, you can access the counter via the 'QueryPerformanceCounter()' function. This returns an arbitrary number that is different on each processor. To find out how many ticks in the counter == 1 second, call 'QueryPerformanceFrequency()'.
在 Windows 中,您可以通过“QueryPerformanceCounter()”函数访问计数器。这将返回一个在每个处理器上不同的任意数字。要找出计数器 == 1 秒中有多少滴答声,请调用“QueryPerformanceFrequency()”。
If you're coding under a platform other than windows, just google performance counter and the system you are coding under, and it should tell you how you can access the counter.
如果您在 Windows 以外的平台下编码,只需 google 性能计数器和您正在编码的系统,它就会告诉您如何访问计数器。
Edit (clarification)
This is c++, just include windows.h and import the "Kernel32.lib" (seems to have removed my hyperlink, check out the documentation at: http://msdn.microsoft.com/en-us/library/ms644904.aspx). For C#, you can use the "System.Diagnostics.PerformanceCounter" class.
编辑(澄清)
这是 C++,只需包含 windows.h 并导入“Kernel32.lib”(似乎已删除我的超链接,请查看以下文档:http://msdn.microsoft.com/en-us/library /ms644904.aspx)。对于 C#,您可以使用“System.Diagnostics.PerformanceCounter”类。
回答by Mr.Ree
Under Linux, try gettimeofday()
for microsecond resolution, or clock_gettime()
for nanosecond resolution.
在 Linux 下,尝试gettimeofday()
使用微秒分辨率或clock_gettime()
纳秒分辨率。
(Of course the actual clock may have a coarser resolution.)
(当然,实际时钟的分辨率可能比较粗糙。)
回答by Amir
In some system you don't have access to the time.h header. Therefore, you can use the following code snippet to find out how long does it take for your program to run, with the accuracy of seconds.
在某些系统中,您无权访问 time.h 标头。因此,您可以使用以下代码片段来了解您的程序运行需要多长时间,精确到秒。
void function()
{
time_t currentTime;
time(¤tTime);
int startTime = currentTime;
/* Your program starts from here */
time(¤tTime);
int timeElapsed = currentTime - startTime;
cout<<"It took "<<timeElapsed<<" seconds to run the program"<<endl;
}