25

我的程序中的一些加载例程需要很长时间才能完成。我想要一个快速的小片段来检查函数执行所需的时间。小我的意思是“最好没有第 3 方库”。

也许像占用系统时间一样简单?

start = current_system_time()
load_something()
delta = current_system_time()-start
log_debug("load took "+delta)

编辑:有问题的目标操作系统是 Windows。

4

5 回答 5

18

你的回答:是的

警告:这不适用于多线程代码或多核机器,您需要一个强大的挂钟计时器。所以我推荐你使用 omp 的挂钟。OMP 包含在 VC 和 GCC 中,大多数编译器和它的标准你不必担心消失

#include <omp.h>

// Starting the time measurement
double start = omp_get_wtime();
// Computations to be measured
...
// Measuring the elapsed time
double end = omp_get_wtime();
// Time calculation (in seconds)
于 2009-01-27T12:51:36.960 回答
14
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

namespace win32 {
    #include <windows.h>
}

class timer
{
    win32::LARGE_INTEGER start_time_;
public:
    timer() { QueryPerformanceCounter( &start_time_ ); }
    void   restart() { QueryPerformanceCounter( &start_time_ ); }
    double elapsed() const
    {
        win32::LARGE_INTEGER end_time, frequency;
        QueryPerformanceCounter( &end_time );
        QueryPerformanceFrequency( &frequency );
        return double( end_time.QuadPart - start_time_.QuadPart )
            / frequency.QuadPart;
    }
};

#else

#include <ctime>

class timer
{
    clock_t _start_time;
public:
    timer() { _start_time = clock(); }
    void   restart() { _start_time = clock(); }
    double elapsed() const
    {
        return double(clock() - _start_time) / CLOCKS_PER_SEC;
    }
};

#endif

template< typename Func >
double measure_time( Func f )
{
    timer t;
    f();
    return t.elapsed();
}
于 2009-01-27T13:19:16.013 回答
5

这是一种对 C/C++ 代码块进行计时的快速而肮脏的方法。你需要#include <sys/time.h>,这应该是一个标准的标题......

struct timeval start, end;
gettimeofday(&start, NULL);
// benchmark code
gettimeofday(&end, NULL);
long long time =   (end.tv_sec * (unsigned int)1e6 +   end.tv_usec) - 
                 (start.tv_sec * (unsigned int)1e6 + start.tv_usec);

这应该在现代 Linux 系统上提供 1-2µs 的分辨率(您使用的是什么操作系统?),这意味着它不太适合学习小于 10µs 的项目。但是,您似乎并不处于那种情况。

更新:基于指定的操作系统... gettimeofday() 的 Windows 实现

于 2009-01-27T12:25:23.783 回答
1

我为此使用了一个类,它旨在测量执行函数所需的时间并将其写入 uth-16le 文本文件(我需要更新它以使用我为此创建的新类......但是 nm)。

在函数顶部创建一个新实例,例如 jProfiler(L"myFunction"),如果您想确定新实例并自己删除它,则函数末尾的清理将完成其余的工作。对于一个小测试来说有点矫枉过正,但可能会有所帮助:

// start header

/* jProfiler class by Semi Essessi
 *
 * (class description goes here)
 *
 */

#ifndef __JPROFILER_H
#define __JPROFILER_H

#include <stdio.h>
#include <windows.h>

class jProfiler
{
private:
    wchar_t*        str;
    LARGE_INTEGER   start;
    LARGE_INTEGER   tps;
    LARGE_INTEGER   buf;

    static FILE*    f;
    static int      instCount;

    static void     Initialise();
    static void     Shutdown();
public:
    jProfiler(const wchar_t* msg);
    ~jProfiler();
};

#endif

// - end header

/* jProfiler class by Semi Essessi
 *
 * (class description goes here)
 *
 */

#include "jProfiler.h"

#include <windows.h>

FILE* jProfiler::f = 0;
int jProfiler::instCount = 0;

jProfiler::jProfiler(const wchar_t* msg)
{
    // constructor code for menuVar
    int i = (int)wcslen(msg)+1;
    str = new wchar_t[i];
    memcpy(str, msg, sizeof(wchar_t)*i);
    str[i-1] =  0;

    QueryPerformanceFrequency(&tps);
    QueryPerformanceCounter(&start);

    instCount++;
    Initialise();
}

jProfiler::~jProfiler()
{
    // destructor code for menuVar
    QueryPerformanceCounter(&buf);
    // work out change in time
    double dt=((float)buf.QuadPart - (float)start.QuadPart)/(float)tps.QuadPart;
    fwprintf(f, L"%s : %.20f\r\n", str, dt);

    if(str) delete[] str;
    instCount--;
    Shutdown();
}

void jProfiler::Initialise()
{
    if(!f)
    {
        f = _wfopen(L"profilerlog.txt", L"wb");
        unsigned short a = 0xFEFF;
        fwrite(&a, sizeof(unsigned short), 1, f);
    }
}

void jProfiler::Shutdown()
{
    if(instCount==0) if(f) fclose(f);
}
于 2009-01-27T13:42:27.947 回答
1

我的sweet.hpp库中有一个benchmark.hpp标头。它有两个基准测试工具。第一个是一个简单的手动启动停止计时

Bench b;
...
b.stop();
b.milli(); // returns an uint with passed millisec. Also has sec and micro sec

另一个更复杂一些。您可以像这样编写函数或块语句。

void myFunc() {
    BENCH(someName);
    ...
}

并最终调用sweet::Benchmark::printResults();以打印所花费的时间和调用次数。

编辑:我添加了一个函数,所以你可以这样调用它。

double c = BENCHMARK_CNT(25, yourFunctionCallHere());
于 2014-02-18T10:54:11.883 回答