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