c++ - Why is the CPU time different with every execution of this program? -


i have hard time understanding processor time. result of program:

#include <iostream> #include <chrono>  // function f() time-consuming work void f() {     volatile long double d;     int size = 10000;     for(int n=0; n<size; ++n)        for(int m=0; m<size; ++m)            d = n*m; }  int main() {     std::clock_t start = std::clock();     f();     std::clock_t end = std::clock();      std::cout << "cpu time used: "               << (end - start)               << "\n"; } 

seems randomly fluctuate between 210 000, 220 000 , 230 000. @ first amazed, why these discrete values. found out std::clock() returns approximate processor time. value returned std::clock() rounded multiple of 10 000. explain why maximum difference between cpu times 20 000 (10 000 == rounding error first call std::clock() , 10 000 second).

but if change int size = 40000; in body of f(), fluctuations in ranges of 3 400 000 3 500 000 cannot explained rounding.

from read clock rate, on wikipedia:

the cpu requires fixed number of clock ticks (or clock cycles) execute each instruction. faster clock, more instructions cpu can execute per second.

that is, if program deterministic (which hope mine is), cpu time needed finish should be:

  1. always same
  2. slightly higher number of instructions carried out

my experiments show neither, since program needs carry out @ least 3 * size * size instructions. please explain doing wrong?

you don't state hardware you're running binary on.

does have interrupt driven cpu ?

is multitasking operating system ?

you're mistaking cycle time of cpu (the cpu clock wikipedia refers to) time takes execute particular piece of code start end , other stuff poor cpu has @ same time.

also ... executing code in level 1 cache, or in level 2 or in main memory, or on disk ... next time run ?


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -