繁体   English   中英

寻找基准测试代码片段(c ++)

[英]Looking for benchmarking code snippet (c++)

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

也许就像花费系统时间一样简单?

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

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

你的回答:是的

警告:这不会在multihtreaded代码或多核计算机上工作,你需要一个强大的挂钟计时器。 所以我建议你使用omp的wallclock。 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)
#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();
}

这是一种快速而又脏的方式来计算一块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实现

我的sweet.hpp库中有一个benchmark.hp头文件。 它有两个基准工具。 第一个是简单的手动启动停止时间接收器

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());

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM