我怎样才能基准C $ C $Ç容易吗? [英] How can I benchmark C code easily?

查看:132
本文介绍了我怎样才能基准C $ C $Ç容易吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的库,以基准它需要执行C code的一部分的时候?我想是这样的:

Is there a simple library to benchmark the time it takes to execute a portion of C code? What I want is something like:

int main(){
    benchmarkBegin(0);
    //Do work
    double elapsedMS = benchmarkEnd(0);

    benchmarkBegin(1)
    //Do some more work
    double elapsedMS2 = benchmarkEnd(1);

    double speedup = benchmarkSpeedup(elapsedMS, elapsedMS2); //Calculates relative speedup
}

这也将是巨大的,如果图书馆让你做很多奔跑,它们平均和定时计算方差!

It would also be great if the library let you do many runs, averaging them and calculating the variance in timing!

推荐答案

基本上,你想要的是一个高分辨率计时器。经过时间当然只是在时间差和所述加速是通过将时间为每个任务计算。我已经包括了code为高分辨率计时器应该至少Windows和UNIX上工作。

Basically, all you want is a high resolution timer. The elapsed time is of course just a difference in times and the speedup is calculated by dividing the times for each task. I have included the code for a high resolution timer that should work on at least windows and unix.

#ifdef WIN32

#include <windows.h>
double get_time()
{
    LARGE_INTEGER t, f;
    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    return (double)t.QuadPart/(double)f.QuadPart;
}

#else

#include <sys/time.h>
#include <sys/resource.h>

double get_time()
{
    struct timeval t;
    struct timezone tzp;
    gettimeofday(&t, &tzp);
    return t.tv_sec + t.tv_usec*1e-6;
}

#endif

这篇关于我怎样才能基准C $ C $Ç容易吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆