基准代码-是否除以迭代次数? [英] Benchmark code - dividing by the number of iterations or not?

查看:88
本文介绍了基准代码-是否除以迭代次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的朋友进行了一次有趣的讨论,关于对C/C ++代码(或一般而言,代码)进行基准测试.我们编写了一个简单的函数,该函数使用getrusage来测量给定代码段的cpu时间. (它测量运行特定功能所花费的cpu时间).让我举一个例子:

I had an interesting discussion with my friend about benchmarking a C/C++ code (or code, in general). We wrote a simple function which uses getrusage to measure cpu time for a given piece of code. (It measures how much time of cpu it took to run a specific function). Let me give you an example:

const int iterations = 409600; 
double s = measureCPU(); 
for( j = 0; j < iterations; j++ )
        function(args); 
double e = measureCPU(); 
std::cout << (e-s)/iterations << " s \n";

我们争论过,是否应该将(e-s)除以迭代次数?我的意思是,当我们不对其进行除法时,结果将处于可接受的形式(例如3.0 s),但是当我们对其进行除法时,其结果将类似于2.34385e-07 s ...

We argued, should we divide (e-s) by the number of iterations, or not? I mean, when we dont divide it the result is in acceptable form (ex. 3.0 s) but when we do divide it, it gives us results like 2.34385e-07 s ...

这是我的问题:

  1. 我们是否应将(e-s)除以迭代次数,如果是,为什么?
  2. 我们如何以更易理解的形式打印2.34385e-07? (比方说,花了0.00000003秒)?
  3. 我们应该首先进行一次函数调用,然后测量迭代的cpu时间,如下所示:

  1. should we divide (e-s) by the number of iterations, if so, why?
  2. how can we print 2.34385e-07 s in more human-readable form? (let's say, it took 0.00000003 s) ?
  3. should we first make a function call for once, and after that measure cpu time for iterations, something like this:

// first function call, doesnt bother with it at all
function(args); 
// real benchmarking
const int iterations = 409600; 
double s = measureCPU(); 
for( j = 0; j < iterations; j++ )
            function(args); 
double e = measureCPU(); 
std::cout << (e-s)/iterations << " s \n";

推荐答案

  1. 如果将时间除以迭代次数,则将获得一个函数运行时间的与迭代无关的比较,迭代次数越多,结果越精确.它在n次迭代中的平均运行时间.
  2. 您可以将除以1e6的时间乘以每一个迭代单位获得微秒(我假设measureCPU返回秒)

  1. if you divide the time by number of iterations, then you'll get iteration independent comparison of run time of one function, the more iterations, the more precise result. its an average run time over n iterations.
  2. you can multiply the divided time by 1e6 to get microseconds per one iteration unit (i assume that measureCPU returns secods)

std::cout << 1e6*(e-s)/iterations << " s \n";

  • 正如@ ogni42所说,您从for循环中获取了测量时间的开销,因此您可以尝试将循环展开一点以降低测量误差,每次迭代进行8到16次调用,然后尝试不同的通话次数以查看测得的时间如何变化:

  • as @ogni42 stated, you are getting an overhead from for loop into your measured time, so you could try to unroll the loop a bit to lower the measurement error, do a 8 to 16 calls each iteration, try different call counts to see how the measured time changes:

    for( j = 0; j < iterations; j++ ) {
        function(args);
        function(args);
        function(args);
        function(args);
        ...
    }
    

  • 您获得的基本数字是较低的是更好的数字.如果您想获得更高的分数会更好,那么您可以 测量功能的不同变化,然后获得最快的时间.这可能得10分.

  • What you basically get is a lower is better number. If you wanted higher is better scoring you could measure diferent variations of function and then get the time of the fastest one. This one could score 10 points.

    score_for_actual_function = 10.0 * fastest_time / time_of_actual_function
    

  • 这种计分方式与时间无关,因此您可以比较不同的函数变体,并且函数可以计分不到一分...并当心被零除:)

    This scoring is kind of time independent, so you can just compare different function variations and the function can score less than one point... and beware of division by zero :)

    这篇关于基准代码-是否除以迭代次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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