Java for loop vs. while loop. Performance difference?

前端 未结 16 1580
长发绾君心
长发绾君心 2020-11-28 09:35

Assume i have the following code, there are three for loop to do something. Would it run fast if i change the most outer for loop to while loop? thanks~~

<         


        
16条回答
  •  -上瘾入骨i
    2020-11-28 10:33

    here's a helpful link to an article on the matter

    according to it, the While and For are almost twice as faster but both are the same.

    BUT this article was written in 2009 and so i tried it on my machine and here are the results:

    • using java 1.7: the Iterator was about 20%-30% faster than For and While (which were still the same)
    • using java 1.6: the Iterator was about 5% faster than For and While (which were still the same)

    so i guess the best thing is to just time it on your own version and machine and conclude from that

提交回复
热议问题