Visual C++ 中的计时程序运行时间

发布于 2024-08-08 17:42:44 字数 232 浏览 3 评论 0原文

是否有一种快速、简单的方法可以对程序的一部分(或整个程序)进行计时,而无需在程序本身内部设置计时器类、函数和变量?

我特别指的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彩虹直至黑白 2024-08-15 17:42:44

timeGetTime 为您提供自此以来经过的毫秒数机器已打开。通过计算该函数在操作之前和之后的结果之间的差异,您可以得到它所花费的毫秒数。不要忘记链接到 winmm.lib

编辑:

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

注意事项:

  1. 此计时器的分辨率通常为 10 毫秒。有多种方法可以更改它,但是对于大多数情况来说它已经足够准确了,
  2. 如果您有多个处理器,则该函数返回的结果在不同处理器上执行可能会不一致

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:

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. 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
抚你发端 2024-08-15 17:42:44

Visual Studio 有一个分析器,但您可能需要比Pro 来访问它(例如,Team System Development Edition)。我不确定分析器是否执行实际计时。从链接的文章来看,它似乎可能只是进行采样,这可以告诉您哪些函数花费的时间最多。

早在 VC6 时代,就有用于分析的命令行工具(PREPPROFILEPREPORT)。这些可以进行计时和采样。构建一个更好的分析器固然很棒,但将其限制在价值数千美元的 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, and PREPORT). 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.

她如夕阳 2024-08-15 17:42:44

Intel 和 AMD CPU 中有一个高速计数器。 Windows API 包括读取该计数器值的函数调用以及计数器的频率,即每秒计数多少次。

以下是如何以微秒为单位计时的示例:


#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;
        }
}

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:


#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;
        }
}

何止钟意 2024-08-15 17:42:44

superuser.com 上的这个答案建议Timethis.exe 来自 Windows 2000 资源工具包工具(仅就像 Unix 系统的时间)。 用法示例

...

程序是用这些运行的
命令:

timethis“perf.exe tt > NUL”
timethis“perfgcc.exe tt > NUL”

时间(文件有 100
行,每行包含250000
或多或少随机的整数
分布在 1 到 250000 之间)
是:

<块引用>

VS编译器:

  • 7.359
  • 7.359
  • 7.296

海湾合作委员会:

  • 1.906
  • 1.906
  • 1.937

...

所以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:

...

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:

VS compiler:

  • 7.359
  • 7.359
  • 7.296

GCC:

  • 1.906
  • 1.906
  • 1.937

...

So Visual Studio is not involved at all.

Another such little utility is ptime.

谜泪 2024-08-15 17:42:44

如果您想在不更改代码的情况下对程序的各个部分进行计时,那么您需要使用分析器。

Visual Studio Team System(或 VS2010 中的 Premium)有一个探查器,但在 Professional 中不可用。其他备受推崇的选项有 AQTime(有 30 天试用期)和 Redgate 的 ANTS 分析器(试用期为两周)。

您可能还想查看 中的建议这个问题提供免费选项,推荐SleepyAMD 适用于 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.

花开雨落又逢春i 2024-08-15 17:42:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文