Hacker News new | ask | show | jobs
by ahpeeyem 1439 days ago

    for (; counter < 1_000; counter++) {
        // ... I know this doesn't make much sense :)
    }
I think the JVM would optimise away this loop because it doesn't do anything. e.g. if the loop optimisation method is to unroll the loop into 1000 inline statements, there's nothing there.

This makes "No Logging" result not quite a fair comparison; maybe something like the sum of 1000 random numbers do some actual work in the loop.

2 comments

Exactly my thoughts when reading the article. A bad micro-benchmark that puts everything in bad context as a result. I have numbers on the impact of logging from other articles that show a significant impact, but nothing at this scale.
In many C/C++ optimizing compiler, this is replaced with `counter = 1000;`.

I have no idea how java handle this.. Does the AOT compiler do any optimization? Would this defer to the JIT?

There is aot native compilation in javaland these days, but I assume you mean javac, the compiler that transforms source into jvm bytecode. It defers pretty much all optimization to the JITs and won't remove empty loops.
No, but in this case, the JIT will get rid of this empty loop.