[Solved] Getting bad performance in C++. What is wrong?


You explicitly ask the compiler to generate unoptimized code with -O0. The result is indeed rather slow as the nested loops do not get optimized away. The java compiler probably does that and removes the empty loops, a rather easy optimization. If you could require the java compiler to generate unoptimized byte code and prevent the runtime from JITing it, the result would certainly be even slower.

You can play with Godbolt’s Compiler Explorer and see the effect of the optimizer, changing the -O0 to -O1, -O2 and -O3: https://godbolt.org/g/CyWuhB

1

solved Getting bad performance in C++. What is wrong?