C / C ++测量程序效率

时间:2016-03-08 02:14:33

标签: c++ c performance

我认为衡量我的程序效率的最佳方法是检查它运行的时间,但是当我这样做的时候,我会随机时间。

我在this这个帖子中使用了Roger Pate所示的以下代码:

#include <ctime>

void func()
{
    using namespace std;
    clock_t begin = clock();

    //Function to measure here

    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}

我认为这是因为我在后台运行了一些程序。

每次我想重新检查我的功能时,我都不想重新启动计算机所以我的问题是:有没有更好的方法来衡量程序的效率?

IDE是Codeblocks,OS是Win7 64bit。

5 个答案:

答案 0 :(得分:3)

使用现代的,千兆赫的CPU和高速RAM I / O,除非您的代码运行时间超过几分钟,否则测量其执行时间不太可能产生任何有意义的结果。信噪比太高了。 I / O中断,自然抢占式多任务处理会产生太多噪音,并淹没任何有意义的运行时指标。

特别是,在您的情况下,在MS Windows上,它不断启动各种后台操作系统。重新启动计算机只会让事情变得更糟。 Windows O / S通常会在重新启动后在接下来的几分钟内启动大量的后台进程,这些进程可以做一件事。通常你需要等待10-15分钟,然后自然背景活动才会平静下来。

答案 1 :(得分:1)

您需要衡量用户时间。您可以使用基于UNIX的系统中的time命令(https://en.wikipedia.org/wiki/Time_(Unix))来完成此操作。我不确定微软是否有类似的东西。

用户时间是操作系统测量过程的时间,因此可以更准确地描述程序运行的时间。它能够做到这一点,因为它是实体调度任务。

答案 2 :(得分:0)

如果您担心I / O中断或抢占式多任务处理的噪音,请使用GetThreadTimes功能。

但是,我会更关心clock()功能的解决方案。在Windows上,我会使用QueryPerformanceCounter函数。

答案 3 :(得分:0)

这是我在这里的第一次行动,所以请善待。

1)如前所述,对于计时器(我们周围)的“较低”精度,函数QueryPerformanceCounter()工作正常。此外,您应该询问此计时器使用QueryPerformanceFrequency()进行踩踏的频率。然后,您可以确定从开始到结束的刻度差异,并将其除以频率。

2)更高的分辨率(也提到)是直接读取机器的TSC(TimeStampCounter)寄存器。命令_rdtscp()是在windows(afaik)下执行此操作的最佳方法。这里的难点在于确定机器当前运行的频率。一种方法是测量用于睡眠的滴答(1000),然后通过简单地将滴答差异作为以Hz为单位的频率来确定频率。

1)的示例代码

--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--

tell application "Finder"
    set the_path to choose folder with prompt "Choose your folder..."
    my file_to_folder(the_path)
end tell

on file_to_folder(the_folder)
    tell application "Finder"

        -- HELP NEEDED HERE
        -- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)
        set the_files to files of the_folder

        repeat with the_file in the_files

            -- Exclude folder in selection
            if kind of the_file is not "Folder" then
                set the_path to container of the_file
                set the_file_ext to name extension of the_file

                -- Remove extension of the file name
                set the_file_name to name of the_file as string
                set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name

                -- Make the new folder with the file name
                try
                    set the_new_folder to make new folder at the_path with properties {name:the_file_name}
                on error
                    -- HELP NEEDED HERE
                    -- HOW TO SET the_new_folder AS THE EXISTING FOLDER
                end try

                -- Move the file in the new folder
                move the_file to the_new_folder

            end if
        end repeat

    end tell
end file_to_folder

tell application "Finder"
    (display dialog ("It's done!") buttons {"Perfect!"})
end tell

2)

#include "Windows.h"

_LARGE_INTEGER Start,Stop,Frequency;

DWORD64 Time_taken;

int YourFunction ()
{
 Start = QueryPerformanceCounter();

 // Run Code to measure here

 Stop = QueryPerformanceCounter();

 Frequency = QueryPerformanceFrequency();

 Time_taken = (Stop.QuadPart-Start.QuadPart)/Frequency.QuadPart;
 }

有关参考文献,请参阅1)https://msdn.microsoft.com/de-de/library/windows/desktop/ms644904%28v=vs.85%29.aspx

和2)https://msdn.microsoft.com/de-de/library/bb385235.aspx

我希望这能回答你的问题。

问候Sascha

答案 4 :(得分:-1)

在Profiler下运行代码,可以优化代码但使用调试信息。

对VerySleepy有很好的经验:http://www.codersnotes.com/sleepy/