Which of these pieces of code is faster in Java?

前端 未结 16 2017
囚心锁ツ
囚心锁ツ 2020-11-30 04:25

a) for(int i = 100000; i > 0; i--) {}

b) for(int i = 1; i < 100001; i++) {}

The answer is t

16条回答
  •  渐次进展
    2020-11-30 05:00

    With regards for testing for zero in the JVM: it can apparently be done with ifeq whereas testing for anything else requires if_icmpeq which also involves putting an extra value on the stack.

    Testing for > 0, as in the question, might be done with ifgt, whereas testing for < 100001 would need if_icmplt.

提交回复
热议问题