【问题标题】:Code benchmarking statistics -代码基准测试统计 -
【发布时间】:2013-07-25 16:46:18
【问题描述】:

正如我在上一个主题中所写:Benchmarking code - am I doing it right? 我需要找到一种方法来获取基准统计信息,例如平均值、平均值、标准差等。我如何使用我发布的这些方法来做到这一点?请注意,我使用一种解决方案来对具有时间间隔的代码进行基准测试,而不是通过多次调用函数。有什么想法吗?

我想出了一个,不知道它是否正确(伪代码):

buffsize = 1024;
buffer [buffsize];
totalcycles = 0

// arrays
walltimeresults = []
cputimeresults = []

// benchmarking
for i in (0, iterations):
   start = walltime();
   fun2measure(args, buffer);
   end = walltime();
   walltimeresults[i] = end - start;

   start = cputime();
   fun2measure(args, buffer);
   end = cputime();
   cputimeresults[i] = end - start;

   c1 = cyclecount();
   fun2measure(args, buffer);
   c2 = cyclecount();

   cyclesperbyte = c2-c1/(buffsize);
   totalcycles += cyclesperbyte;

for i in range (0, iterations) : sum += walltimeresults[i];
avg_wall_time = sum / iterations;

sum = 0;

for i in range (0, iterations) : sum += cputimeresults[i];
avg_cpu_time = sum / iterations;

avg_cycles = totalcycles / iterations;

正确吗?均值、标准差等呢?

【问题讨论】:

    标签: c performance benchmarking


    【解决方案1】:

    你的平均水平看起来不错。

    平均值(即平均值)是

    mean = 1/N * sum( x[i] )
    

    标准差是方差的平方根:

    sigma = sqrt( 1/N * sum( (x[i]-mean)^2 )
    

    【讨论】:

    • 谢谢!关于代码的任何其他建议?我可以在这里改进、改变什么等?
    • @nullpointer:我会弹出一个关卡,问你的总体目的是什么?如果它与找到最快的算法有关,我就不太关心测量了。我会更关心深入了解如何使程序更快。如果这是目标,你might find this helpful。您会惊讶于所谓的最佳计划可以减少多少脂肪。
    • 再次感谢您。我会阅读这个主题,但我的目的不是找到最快的算法,而是衡量它的性能,仅此而已。我知道单个测试(一个函数调用)在基准测试中毫无意义。我只是想知道我是否做对了,我的测量结果是否“可用”。
    • 另外,我想问你,如果函数调用的时间太“短”,以至于单个调用在每次迭代中显示0.00000秒,我该怎么办?我可以这样做:pastie.org/private/r2oxfjfwohupg1ypcjxgnw,对吗?
    • @nullpointer:我喜欢堆栈采样,但即使gprof 也应该告诉你包含时间。本质上,如果整个程序需要 100 秒,并且在那段时间内函数 foo 出现在 10% 的堆栈样本上,那么它所花费的总包含时间是 100 的 10% 或 10 秒。如果你将它除以它被调用的次数,那就是你每次调用的时间。如果它运行的时间太短而无法获取样本,只需围绕它进行大约 1000 次迭代的循环。各种分析器可以自动获取堆栈样本,例如 oprofile 和 Zoom
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 2012-01-15
    • 2023-02-19
    • 2013-03-10
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多