简体   繁体   English

在 Visual C++ 中计时程序运行时

[英]Timing program runtimes in visual C++

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).我特别指的是 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.编辑:这些答案都没有满足我的要求,我希望能够在 Visual c++ 中为程序计时,而不必在其中编写额外的代码位。 Similar to how people do it with BASH in Linux.类似于人们在 Linux 中使用 BASH 的方式。

timeGetTime gives you number of milliseconds passed since the machine was turned on. timeGetTime为您提供自机器开启以来经过的毫秒数。 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不要忘记链接 winmm.lib

EDIT:编辑:

DWORD msBegin = timeGetTime();
// do your stuff here
DWORD msDuration = timeGetTime() - msBegin;
// at this moment msDuration contains number of milliseconds it took for your stuff to execute

Caveats:注意事项:

  1. the resolution of this timer is usually 10msec.此计时器的分辨率通常为 10 毫秒。 There are ways to change it, but it's accurate enough for most of the scenarios有一些方法可以改变它,但它对于大多数场景来说已经足够准确了
  2. if you have several processors, the results returned by this function, executed on different processors may be inconsistent如果你有多个处理器,这个函数返回的结果,在不同的处理器上执行可能会不一致

Visual Studio has a profiler , but you might need a higher SKU than Pro to access it (eg, Team System Development Edition). Visual Studio 有一个分析器,但您可能需要比 Pro 更高的 SKU 才能访问它(例如,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 , and PREPORT ).回到 VC6 时代,有用于分析的命令行工具( PREPPROFILEPREPORT )。 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.构建更好的分析器固然很棒,但将其限制为价值数千美元的 SKU 会使较小的 ISV 和业余爱好者望而却步。

In the Intel and AMD CPUs there is a high speed counter.在 Intel 和 AMD CPU 中有一个高速计数器。 The Windows API includes function calls to read the value of this counter and also the frequency of the counter - ie how many times per second it is counting. Windows API 包括函数调用以读取此计数器的值以及计数器的频率 - 即每秒计数多少次。

Here's an example how to time your time in microseconds:以下是如何以微秒为单位计时的示例:


#include <iostream>
#include <windows.h>

int main()
{
        __int64 ctr1 = 0, ctr2 = 0, freq = 0;

        // Start timing the code.

        if (QueryPerformanceCounter((LARGE_INTEGER *) &ctr1) != 0) {
                // Do what ever you do, what ever you need to time...
                //
                //
                //

                // Finish timing the code.

                QueryPerformanceCounter((LARGE_INTEGER *) &ctr2);
                QueryPerformanceFrequency((LARGE_INTEGER *) &freq);

                // Print the time spent in microseconds to the console.

                std::cout << ((ctr2 - ctr1) * 1.0 / freq) << std::endl;
        }
}

This answer at superuser.com suggests Timethis.exe from Windows 2000 Resource Kit Tool (just like time for Unix systems). superuser.com 上的这个答案建议使用Windows 2000 Resource Kit Tool 中的Timethis.exe (就像 Unix 系统的时间)。 Example usage : 示例用法

... ...

The programs were run with these commands:这些程序是用这些命令运行的:

 timethis "perf.exe tt > NUL" timethis "perfgcc.exe tt > NUL"

The timings (with a file having 100 lines, each line containing 250000 integers more or less randomly distributed between 1 and 250000) were:时间(文件有 100 行,每行包含 250000 个整数,或多或少随机分布在 1 和 250000 之间)是:

VS compiler: VS编译器:

  • 7.359 7.359
  • 7.359 7.359
  • 7.296 7.296

GCC:海湾合作委员会:

  • 1.906 1.906
  • 1.906 1.906
  • 1.937 1.937

... ...

So Visual Studio is not involved at all.所以根本不涉及 Visual Studio。

Another such little utility is ptime .另一个这样的小工具是ptime

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. Visual Studio Team System(或 VS2010 中的 Premium)有一个分析器,但它在 Professional 中不可用。 Other well regarded options are AQTime (which has 30 day trial) and Redgate's ANTS profiler (which has a two week trial).其他备受推崇的选择是AQTime (有 30 天的试用期)和Redgate 的 ANTS profiler (有两周的试用期)。

You may also want to look at the suggestions in this question for free options, which recommends Sleepy and AMD's CodeAnalyst for Windows .您可能还想查看此问题中有关免费选项的建议,其中推荐SleepyAMD 的 CodeAnalyst for Windows

In VS19 you can just start debugging and use PerfTips .在 VS19 中,您可以开始调试并使用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.通过打开诊断工具窗口中的事件选项卡,您可以更好地了解这一点。

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

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