我正在做一个性能比较测试。我想记录我的 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?
发布评论
评论(8)
如果您使用的是 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).我认为您要问的是“如何测量进程运行所需的时间,而不考虑‘外部’因素,例如系统上运行的其他程序?”在这种情况下,最简单的方法就是多次运行该程序,并获得平均时间。这样您就可以进行更有意义的比较,希望操作系统花费 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.
您可以将其放入
并完成此操作
int main()
方法中 。当然,您必须将差异返回到日志或cout
。You can put this
and finish with this
in your
int main()
method. Naturally you'll have to return the difference either to a log orcout
it.只是为了扩展 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
如果您使用的是 Windows 系统,则可以通过调用 QueryPerformanceCounter():
...作为一个额外的好处,这里有一个函数将经过的时间(以秒为单位,浮点)转换为描述性字符串:
If you're on a Windows system you can use the high-performance counters by calling QueryPerformanceCounter():
...and just as a bonus, here's a function that converts the elapsed time (in seconds, floating point) to a descriptive string:
下载 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.
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.
您还可以使用程序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