0

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:

  1. What is cause of the randomness (variation)? Note that I ran the application while CPU usage was 2-4% and memory was 10%.
  2. 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;
}
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • Take a look at the task manager and you'll see a hundred tasks, most of which are sleeping at any point in time. But if 4 of them wake up at the same time (using all 4 CPU cores), then your app can be temporarily suspended. The performance counter, meanwhile, will continue to run. – user3386109 Jan 20 '18 at 04:02
  • Yes, that is correct. – user3386109 Jan 20 '18 at 04:08
  • Micro$oft Windows is not a real-time operating system. There's scads of stuff going on besides your program, and lots of ways your program can get a cache or pipeline miss, or even hit the page file. – Jive Dadson Jan 20 '18 at 07:00
  • See answer - https://stackoverflow.com/a/37445086/2785528 – 2785528 Jan 21 '18 at 04:00

1 Answers1

1

A general-purpose computing system has many tasks going on. At any moment, the system may have to respond to I/O interrupts (disk drive completion notices, timer interrupts, network activity,…) and run various housekeeping tasks (background backups, check for scheduled events, indexing user files,…).

The times at which they occur are effectively random. Measuring execution time repeatedly and discarding outliers is a common technique.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312