I got result of time measurements below for repeated computations for simple summation from my Windows machine with 3.2Ghz quad-core CPU and 24GB RAM.
The code is following.
From the result, the summation takes less than 3 ms most of time but sometimes it can take 20 times more. I can understand the large maximum because distribution of the time measurements is exponential having very long right tail.
But what I am not sure of are:
- What is cause of the randomness (variation)? Note that I ran the application while CPU usage was 2-4% and memory was 10%.
- Possible solution for the randomness. Is there any way to avoid the rare maximum duration?
Results
Time Statistics (ms)
N : 10000
Minimum: 2.31406
Maximum: 64.7171
Mean : 2.43556
Std : 0.676273
M+6Std : 3.11184
Code:
#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;
}