-1

I am doing some experiments on CPU's performance. I wonder if anyone know a formal way or a tool to generate simple code that can run for a period of time (several seconds) and consumes significant computation resource of a CPU.

I know there are a lot of CPU benchmarks but the code of them is pretty complicated. What I want is a program more straight forward.

As the compiler is very smart, writing some redundant code as following will not work.

for (int i = 0; i < 100; i++) {
    int a = i * 200 + 100;
}
RandyTek
  • 4,374
  • 3
  • 23
  • 32

2 Answers2

1

It's not necessarily your optimiser that removes the code. CPU's these days are very powerful, and you need to increase the challenge level. However, note that your original code is not a good general benchmark: you only use a very subset of a CPU's instruction set. A good benchmark will try to challenge the CPU on different kinds of operations, to predict the performance in real world scenarios. Very good benchmarks will even put load on various components of your computer, to test their interplay.

Therefore, just stick to a well known published benchmark for your problem. There is a very good reason why they are more involved. However, if you really just want to benchmark your setup and code, then this time, just go for higher counter values:

  double j=10000;

  for (double i = 0; i < j*j*j*j*j; i++) 
  {
  }

This should work better for now. Note that there a just more iterations. Change j according to your needs.

muenalan
  • 588
  • 4
  • 8
1

Put the benchmark code in a function in a separate translation unit from the code that calls it. This prevents the code from being inlined, which can lead to aggressive optimizations.

Use parameters for the fixed values (e.g., the number of iterations to run) and return the resulting value. This prevents the optimizer from doing too much constant folding and it keeps it from eliminating calculations for a variable that it determines you never use.

Building on the example from the question:

int TheTest(int iterations) {
    int a;
    for (int i = 0; i < iterations; i++) {
        a = i * 200 + 100;
    }
    return a;
}

Even in this example, there's still a chance that the compiler might realize that only the last iteration matters and completely omit the loop and just return 200*(iterations - 1) + 100, but I wouldn't expect that to happen in many real-life cases. Examine the generated code to be certain.

Other ideas, like using volatile on certain variables can inhibit some reasonable optimizations, which might make your benchmark perform worse that actual code.

There are also frameworks, like this one, for writing benchmarks like these.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175