java - Why does decrement loop runs faster than increment loop? -
this question has answer here:
- loop counter in java api 3 answers
i going through increment/decrement operators, , i encountered if run loop in decremental form in case run faster same loop in incremental form. expecting both take equal time since same amount of steps followed. searched through web not find convincing answer this. due case decrement operator takes less time compared increment operator?
for(int = 100000; > 0; i--) {} for(int = 1; < 100001; i++) {}
this because in bytecode comparison 0 different operation comparison non-zero number. i < 10001
requires first load number on stack execute comparison, while i > 0
executed 1 operation. of course there no speed difference in cases because of jvm optimizations. can try make visible running code -xint option (interpreted mode execution only).
Comments
Post a Comment