如何获得系统时间在C ++? [英] How to get system time in C++?

查看:104
本文介绍了如何获得系统时间在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事实上,我试图计算函数在我的程序中完成的时间。
所以我使用逻辑来获取系统时间,当我调用函数和时间,当函数返回一个值,然后减去我得到的时间,它完成了所需的值。
所以如果任何人可以告诉我一些更好的方法或者如何获得系统时间在一个实例,这将是相当帮助

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

推荐答案

您的问题完全取决于您使用的WHICH系统。每个系统都有自己的功能来获取当前时间。要了解系统运行的时间,您需要访问一个高分辨率性能计数器。如果你不使用性能计数器,你通常被限制在微秒精度(或更差),这在几乎没有用于分析功能的速度。

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.

在Windows ,您可以通过QueryPerformanceCounter()函数访问计数器。这返回在每个处理器上不同的任意数。要了解计数器中有多少刻度== 1秒,请调用QueryPerformanceFrequency()。

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以外的平台下编码,性能计数器和您正在编码的系统,并且应该告诉您如何访问计数器。

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.

编辑(澄清)

这是c ++,只是包括windows.h并导入Kernel32.lib(似乎已经删除了我的超链接,查阅文档:http:// msdn。 microsoft.com/en-us/library/ms644904.aspx)。对于C#,您可以使用System.Diagnostics.PerformanceCounter类。

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 ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆