我得到了下面的时间测量结果,用于重复计算,从我的Windows机器上使用3.2Ghz四核CPU和24GB RAM进行简单求和。
代码如下。
从结果来看,总和大部分时间不到3毫秒,但有时可能需要20倍。我可以理解大的最大值,因为时间测量的分布是指数的,具有非常长的右尾。
但我不确定的是:
结果
Time Statistics (ms)
N : 10000
Minimum: 2.31406
Maximum: 64.7171
Mean : 2.43556
Std : 0.676273
M+6Std : 3.11184
代码:
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
int main()
{
LARGE_INTEGER t_start, t_end, Frequency;
double tdiff,minx=1e+307,maxx=-1e+307,meanx=0,stdx=0;
int niter = 10000;
for (int j = 0;j < niter;j++)
{
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&t_start);
double s = 0;
for (int i = 0;i < 1000000;i++) s += i;
QueryPerformanceCounter(&t_end);
tdiff = (double)(t_end.QuadPart - t_start.QuadPart) / (double)Frequency.QuadPart * 1000;
minx = min(minx, tdiff);
maxx = max(maxx, tdiff);
meanx += tdiff;
stdx += tdiff*tdiff;
//std::cout << "Iteration: " << j << " Time (ms): " << tdiff << std::endl;
}
meanx /= (double)niter;
stdx = sqrt((stdx - (double)niter*meanx*meanx) / (double)(niter - 1));
std::cout << "Time Statistics (ms) " << std::endl << std::endl;
std::cout << "N : " << niter << std::endl;
std::cout << "Minimum: " << minx << std::endl;
std::cout << "Maximum: " << maxx << std::endl;
std::cout << "Mean : " << meanx << std::endl;
std::cout << "Std : " << stdx << std::endl;
std::cout << "M+6Std : " << meanx+stdx << std::endl;
return 0;
}
答案 0 :(得分:1)
通用计算系统正在进行许多任务。在任何时候,系统可能必须响应I / O中断(磁盘驱动器完成通知,计时器中断,网络活动......)并运行各种内务处理任务(后台备份,检查预定事件,索引用户文件......)。
它们发生的时间实际上是随机的。反复测量执行时间并丢弃异常值是一种常见的技术。