Visual C++ 中的计时程序运行时间
是否有一种快速、简单的方法可以对程序的一部分(或整个程序)进行计时,而无需在程序本身内部设置计时器类、函数和变量?
我特别指的是 Visual C++ (Professional 2008)。
谢谢,
-Faken
编辑:这些答案都不能满足我的要求,我希望能够在 Visual C++ 中对程序进行计时,而不必在其中编写额外的代码。类似于人们在 Linux 中使用 BASH 的方式。
Is there a quick and easy way of timing a section of a program (or the entire thing) without having to setup a timer class, functions, and variables inside my program itself?
I'm specifically referring to Visual C++ (Professional 2008).
Thanks,
-Faken
Edit: none of these answers do what i ask for, i would like to be able to time a program inside visual c++ WITHOUT having to write extra bits of code inside it. Similar to how people do it with BASH in Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
timeGetTime 为您提供自此以来经过的毫秒数机器已打开。通过计算该函数在操作之前和之后的结果之间的差异,您可以得到它所花费的毫秒数。不要忘记链接到 winmm.lib
编辑:
注意事项:
timeGetTime gives you number of milliseconds passed since the machine was turned on. By calculating the difference between the results of this function before and after your operation, you get the number of milliseconds it took. Don't forget to link with winmm.lib
EDIT:
Caveats:
Visual Studio 有一个分析器,但您可能需要比Pro 来访问它(例如,Team System Development Edition)。我不确定分析器是否执行实际计时。从链接的文章来看,它似乎可能只是进行采样,这可以告诉您哪些函数花费的时间最多。
早在 VC6 时代,就有用于分析的命令行工具(
PREP
、PROFILE
和PREPORT
)。这些可以进行计时和采样。构建一个更好的分析器固然很棒,但将其限制在价值数千美元的 SKU 范围内会让规模较小的 ISV 和爱好者望而却步。Visual Studio has a profiler, but you might need a higher SKU than Pro to access it (e.g., Team System Development Edition). I'm not sure if the profiler does actual timings. From the linked article, it looks as though it might just do sampling, which can tell you which functions are taking the most time.
Back in the VC6 days, there were command line tools for profiling (
PREP
,PROFILE
, andPREPORT
). Those could do timing as well as sampling. Building a better profiler is great, but restricting it to the multi-thousand dollar SKUs keeps smaller ISVs and hobbyists out of the game.Intel 和 AMD CPU 中有一个高速计数器。 Windows API 包括读取该计数器值的函数调用以及计数器的频率,即每秒计数多少次。
以下是如何以微秒为单位计时的示例:
In the Intel and AMD CPUs there is a high speed counter. The Windows API includes function calls to read the value of this counter and also the frequency of the counter - i.e. how many times per second it is counting.
Here's an example how to time your time in microseconds:
superuser.com 上的这个答案建议Timethis.exe 来自 Windows 2000 资源工具包工具(仅就像 Unix 系统的时间)。 用法示例:
所以Visual Studio根本不参与。
另一个这样的小实用程序是ptime。
This answer at superuser.com suggests Timethis.exe from Windows 2000 Resource Kit Tool (just like time for Unix systems). Example usage:
So Visual Studio is not involved at all.
Another such little utility is ptime.
如果您想在不更改代码的情况下对程序的各个部分进行计时,那么您需要使用分析器。
Visual Studio Team System(或 VS2010 中的 Premium)有一个探查器,但在 Professional 中不可用。其他备受推崇的选项有 AQTime(有 30 天试用期)和 Redgate 的 ANTS 分析器(试用期为两周)。
您可能还想查看 中的建议这个问题提供免费选项,推荐Sleepy和AMD 适用于 Windows 的 CodeAnalyst。
If you want to time sections of your program without changing the code then you need to get hold of a profiler.
Visual Studio Team System (or Premium in VS2010) has a profiler but it's not available in Professional. Other well regarded options are AQTime (which has 30 day trial) and Redgate's ANTS profiler (which has a two week trial).
You may also want to look at the suggestions in this question for free options, which recommends Sleepy and AMD's CodeAnalyst for Windows.
在 VS19 中,您可以开始调试并使用 PerfTips。该行末尾的灰色文本将告诉您自上次休息以来已经过去了多少时间。通过打开诊断工具窗口中的事件选项卡,您可以更好地了解这一情况。
In VS19 you can just start debugging and use PerfTips. The grey text in the end of the line will tell you how much time has elapsed since the last break. You can get a better picture at this by opening the Events tab in Diagnostic Tools window.