在Windows上使用C ++计算CPU时间 [英] Computing CPU time in C++ on Windows

查看:461
本文介绍了在Windows上使用C ++计算CPU时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中有没有办法计算在 CPU时间中运行给定程序或例程需要多长时间?

Is there any way in C++ to calculate how long does it take to run a given program or routine in CPU time?

使用在Windows 7上运行的Visual Studio 2008.

I work with Visual Studio 2008 running on Windows 7.

推荐答案

如果你想知道一个进程使用的CPU总时间, clock rdtsc (直接或通过编译器本身)是真正最好的选择,至少IMO。如果你需要的代码是可移植的,你可以做的最好的是使用 clock ,测试与系统尽可能静止,并希望最好的(但如果你do,请注意 clock 的解析度是 CLOCKS_PER_SEC ,它可能或可能不是1000,即使它是的,你的实际时间分辨率通常不会那么好 - 它可能给你的时间在毫秒,但至少通常提前几十毫秒一次)。

If you want to know the total amount of CPU time used by a process, neither clock nor rdtsc (either directly or via a compiler intrinsic) is really the best choice, at least IMO. If you need the code to be portable, about the best you can do is use clock, test with the system as quiescent as possible, and hope for the best (but if you do, be aware that the resolution of clock is CLOCKS_PER_SEC, which may or may not be 1000, and even if it is, your actual timing resolution often won't be that good -- it may give you times in milliseconds, but at least normally advance tens of milliseconds at a time).

因为,但是,你似乎并不介意代码是特定于Windows,你可以做得更好一点。至少如果我对你正在寻找的东西的理解是正确的,你真正想要的可能是 GetProcessTimes ,它将(单独)告诉你进程的内核模式和用户模式CPU使用开始时间和退出时间,从中可以计算使用的墙面时间,如果你在乎)。还有 QueryProcessCycleTime ,它将告诉您进程使用的CPU时钟周期总数(所有线程中用户和内核模式的总和)。就个人而言,我有一个很难想象的后者的很多用处,虽然 - 计数单个时钟周期可以用于代码段,需要密集优化,但我不太确定如何' d应用于一个完整的过程。 GetProcessTimes 使用FILETIME结构,它支持100纳秒的分辨率,但在实际中大多数时候,你会看到的是调度程序的时间片的倍数(它随窗口版本而变化,但是在毫秒到几十毫秒的量级)。

Since, however, you don't seem to mind the code being specific to Windows, you can do quite a bit better. At least if my understanding of what you're looking for is correctly, what you really want is probably GetProcessTimes, which will (separately) tell you both kernel-mode and user-mode CPU usage of the process (as well as the start time and exit time, from which you can compute wall time used, if you care). There's also QueryProcessCycleTime, which will tell you the total number of CPU clock cycles used by the process (total of both user and kernel mode in all threads). Personally, I have a hard time imagining much use for the latter though -- counting individual clock cycles can be useful for small sections of code subject to intensive optimization, but I'm less certain about how you'd apply it to a complete process. GetProcessTimes uses FILETIME structures, which support resolutions of 100 nanoseconds, but in reality most times you'll see will be multiples of the scheduler's time slice (which varies with the version of windows, but is on the order of milliseconds to tens of milliseconds).

在任何情况下,如果你真的想从开始到结束的时间, GetProcessTimes 会让你这样做 - 如果你生成程序(例如, CreateProcess ),你会得到一个处理过程,当子进程退出时发出信号。然后,您可以在该句柄上调用 GetProcessTimes ,并且即使子进程已经退出也会检索时间 - 只要至少有一个进程句柄,句柄就会保持有效保持打开。

In any case, if you truly want time from beginning to end, GetProcessTimes will let you do that -- if you spawn the program (e.g., with CreateProcess), you'll get a handle to the process which will be signaled when the child process exits. You can then call GetProcessTimes on that handle, and retrieve the times even though the child has already exited -- the handle will remain valid as long as at least one handle to the process remains open.

这篇关于在Windows上使用C ++计算CPU时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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