Windows:如何计算 ac/c++ 应用程序运行所需的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2105411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:48:10  来源:igfitidea点击:

Windows: How do I calculate the time it takes a c/c++ application to run?

c++cwindowstestingruntime

提问by Brian T Hannan

I am doing a performance comparison test. I want to record the run time for my c++ test application and compare it under different circumstances. The two cases to be compare are: 1) a file system driver is installed and active and 2) also when that same file system driver is not installed and active.

我正在做性能比较测试。我想记录我的 c++ 测试应用程序的运行时间并在不同情况下进行比较。要比较的两种情况是:1) 文件系统驱动程序已安装并处于活动状态 2) 同样的文件系统驱动程序未安装且处于活动状态时。

A series of tests will be conducted on several operating systems and the two runs described above will be done for each operating system and it's setup. Results will only be compared between the two cases for a given operating system and setup.

将在多个操作系统上进行一系列测试,并且将对每个操作系统及其设置进行上述两次运行。结果将仅在给定操作系统和设置的两种情况下进行比较。

I understand that when running a c/c++ application within an operating system that is not a real-time system there is no way to get the real time it took for the application to run. I don't think this is a big concern as long as the test application runs for a fairly long period of time, therefore making the scheduling, priorities, switching, etc of the CPU negligible.

我知道在非实时系统的操作系统中运行 ac/c++ 应用程序时,无法获得应用程序运行所需的实时时间。我不认为这是一个大问题,只要测试应用程序运行相当长的一段时间,从而使 CPU 的调度、优先级、切换等可以忽略不计。

Edited: For Windows platform only How can I generate some accurate application run time results within my test application?

编辑:仅适用于 Windows 平台如何在我的测试应用程序中生成一些准确的应用程序运行时结果?

采纳答案by wheaties

You can put this

你可以把这个

#if _DEBUG
time_t start = time(NULL);
#endif

and finish with this

并以这个结束

#if _DEBUG
time end = time(NULL);
#endif

in your int main()method. Naturally you'll have to return the difference either to a log or coutit.

在你的int main()方法中。当然,您必须将差异返回到日志或cout它。

回答by ezod

If you're on a POSIX system you can use the timecommand, which will give you the total "wall clock" time as well as the actual CPU times (user and system).

如果您使用的是 POSIX 系统,则可以使用该time命令,该命令将为您提供总的“挂钟”时间以及实际的 CPU 时间(用户和系统)。

Edit: Apparently there's an equivalent for Windows systems in the Windows Server 2003 Resource Kitcalled timeit.exe(not verified).

编辑:显然在Windows Server 2003 Resource Kit 中有一个 Windows 系统的等效项称为timeit.exe(未验证)。

回答by Dima

I think what you are asking is "How do I measure the time it takes for the process to run, irrespective of the 'external' factors, such as other programs running on the system?" In that case, the easiest thing would be to run the program multiple times, and get an average time. This way you can have a more meaningful comparison, hoping that various random things that the OS spends the CPU time on will average out. If you want to get real fancy, you can use a statistical test, such as the two-sample t-test, to see if the difference in your average timings is actually significant.

我想您要问的是“我如何衡量进程运行所需的时间,而不管‘外部’因素如何,例如系统上运行的其他程序?” 在这种情况下,最简单的方法是多次运行该程序,并获得平均时间。通过这种方式,您可以进行更有意义的比较,希望操作系统花费 CPU 时间的各种随机事物会平均化。如果您想获得真正的幻想,可以使用统计检验(例如双样本 t 检验)来查看平均时间的差异是否确实显着。

回答by Martin Beckett

Just to expand on ezod's answer.
You run the program with the time command to get the total time - there are no changes to your program

只是为了扩展ezod的答案。
您使用 time 命令运行程序以获得总时间 - 您的程序没有变化

回答by John Dibling

If you're on a Windows system you can use the high-performance counters by calling QueryPerformanceCounter():

如果您使用的是 Windows 系统,则可以通过调用QueryPerformanceCounter()来使用高性能计数器:

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

int main()
{
    LARGE_INTEGER li = {0}, li2 = {0};
    QueryPerformanceFrequency(&li);
    __int64 freq = li.QuadPart;

    QueryPerformanceCounter(&li);
        // run your app here...
    QueryPerformanceCounter(&li2);

    __int64 ticks = li2.QuadPart-li.QuadPart;
    cout << "Reference Implementation Ran In " << ticks << " ticks" << " (" << format_elapsed((double)ticks/(double)freq) << ")" << endl;
    return 0;
}

...and just as a bonus, here's a function that converts the elapsed time (in seconds, floating point) to a descriptive string:

...作为奖励,这是一个将经过时间(以秒为单位,浮点数)转换为描述性字符串的函数:

std::string format_elapsed(double d) 
{
    char buf[256] = {0};

    if( d < 0.00000001 )
    {
        // show in ps with 4 digits
        sprintf(buf, "%0.4f ps", d * 1000000000000.0);
    }
    else if( d < 0.00001 )
    {
        // show in ns
        sprintf(buf, "%0.0f ns", d * 1000000000.0);
    }
    else if( d < 0.001 )
    {
        // show in us
        sprintf(buf, "%0.0f us", d * 1000000.0);
    }
    else if( d < 0.1 )
    {
        // show in ms
        sprintf(buf, "%0.0f ms", d * 1000.0);
    }
    else if( d <= 60.0 )
    {
        // show in seconds
        sprintf(buf, "%0.2f s", d);
    }
    else if( d < 3600.0 )
    {
        // show in min:sec
        sprintf(buf, "%01.0f:%02.2f", floor(d/60.0), fmod(d,60.0));
    }
    // show in h:min:sec
    else 
        sprintf(buf, "%01.0f:%02.0f:%02.2f", floor(d/3600.0), floor(fmod(d,3600.0)/60.0), fmod(d,60.0));

    return buf;
}

回答by Diomidis Spinellis

Download Cygwinand run your program by passing it as an argument to the timecommand. When you're done, spend some time to learn the rest of the Unix tools that come with Cygwin. This will be one of the best investments for your career you'll ever make; the Unix toolchest is a timeless classic.

下载Cygwin并通过将其作为参数传递给time命令来运行您的程序。完成后,花一些时间学习 Cygwin 附带的其余 Unix 工具。这将是您职业生涯中最好的投资之一;Unix 工具箱是永恒的经典。

回答by sack

QueryPerformanceCounter can have problems on multicore systems, so I prefer to use timeGetTime() which gives the result in milliseconds

QueryPerformanceCounter 在多核系统上可能会出现问题,所以我更喜欢使用 timeGetTime(),它以毫秒为单位给出结果

you need a 'timeBeginPeriod(1)' before and 'timeEndPeriod(1)' afterwards to reduce the granularity as far as you can but I find it works nicely for my purposes (regulating timesteps in games), so it should be okay for benchmarking.

你需要一个 'timeBeginPeriod(1)' 之前和 'timeEndPeriod(1)' 来尽可能地减少粒度,但我发现它很适合我的目的(调节游戏中的时间步长),所以基准测试应该没问题.

回答by oosterwal

You can also use the program very sleepyto get a bunch of runtime information about your program. Here's a link: http://www.codersnotes.com/sleepy

您还可以使用非常困的程序来获取有关您的程序的一堆运行时信息。这是一个链接:http: //www.codersnotes.com/sleepy