Search code examples
c++cpubenchmarkingtiming

How to test the CPU with a simple C++ program


In my opinion, I have two ways to do this:
One is to use a loop:

t1 = getTime();
for(int i = 0; i < 9999999; i++)
{}
t2 = getTime();

And now I can check t2 - t1.

The other is to create many threads, each thread does the same loop above.

I don't know if the second way is better than the first one.

Or maybe there are better ways to test CPU?


Solution

  • This loop does nothing and will be optimized out by every reasonable compiler.

    To get reasonable benchmark results, you'll have to solve (somewhat) reasonable problems, like calculating many digits of pi, finding big primes or whatever task(s) you want to base your definition of fast on, with good implementations of efficient algorithms.