A loop with an empty body in Java

前端 未结 4 1776
时光说笑
时光说笑 2021-01-18 06:53

During bug fixing in very old project I\'ve faced with strange method, it looks like this:

   void waiter() {
        for (int i = 0; i < 20000; i++) ;
           


        
4条回答
  •  迷失自我
    2021-01-18 07:23

    I don't know if it has changed, I haven't used java for 2 years but it doesn't seem to.

    http://www.herongyang.com/JVM/Benchmark-Int-Empty-Loop-16-Nanosecond.html http://www.herongyang.com/JVM/Benchmark-Long-Empty-Loop-25-Nanosecond.html

    This test also confirms that the Java bytecode compiler "javac" is not doing any optimization to replacing the empty loop with "i=steps" which is the net effect of the loop.

提交回复
热议问题