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

发布于 2024-08-18 23:28:11 字数 356 浏览 3 评论 0 原文

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

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

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

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

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

诗化ㄋ丶相逢 2024-08-25 23:28:11

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

编辑:显然 Windows Server 2003 资源工具包,名为 timeit.exe(未验证)。

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

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

人│生佛魔见 2024-08-25 23:28:11

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

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.

允世 2024-08-25 23:28:11

您可以将其放入

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

并完成此操作

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

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

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 cout it.

悲欢浪云 2024-08-25 23:28:11

只是为了扩展 ezod 的答案。
您使用 time 命令运行程序来获取总时间 - 您的程序没有更改

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

我不会写诗 2024-08-25 23:28:11

如果您使用的是 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;
}

...作为一个额外的好处,这里有一个函数将经过的时间(以秒为单位,浮点)转换为描述性字符串:

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

If you're on a Windows system you can use the high-performance counters by calling 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;
}
猫七 2024-08-25 23:28:11

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

Download Cygwin and run your program by passing it as an argument to the time command. 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.

穿透光 2024-08-25 23:28:11

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

您需要在之前使用“timeBeginPeriod(1)”,在之后使用“timeEndPeriod(1)”,以尽可能减少粒度,但是我发现它非常适合我的目的(调节游戏中的时间步长),所以它应该可以用于基准测试。

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

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.

演出会有结束 2024-08-25 23:28:11

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

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

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