正如我在上一篇文章中写的那样: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;
这是对的吗?平均值,标准偏差等怎么样?
答案 0 :(得分:2)
你的平均看起来还不错。
平均值(即平均值)
mean = 1/N * sum( x[i] )
标准差是方差的平方根:
sigma = sqrt( 1/N * sum( (x[i]-mean)^2 )