Java test: Iterator foreach for three iterations in what way is it faster?

Source: Internet
Author: User

Code:

 Public classMain { Public Static voidMain (string[] p_args) {ArrayList<String> _l_string =NewArraylist<>();  for(into_a = 0; O_a < 100000; o_a++) {_l_string.add (string.valueof (o_a)); } c_public_counttime _counttime=NewC_public_counttime ();        _counttime.f_begin ();  for(inti = 0; I < _l_string.size (); i++) {System.out.print (_l_string.get (i)); } String _time_for=_counttime.f_done ();        _counttime.f_begin ();  for(String o_a: _l_string) {System.out.print (o_a); } String _time_foreach=_counttime.f_done ();        _counttime.f_begin ();  for(Iterator<string> O_iterator =_l_string.iterator (); O_iterator.hasnext ();)        {System.out.print (O_iterator.next ()); } String _time_iterator=_counttime.f_done (); System.out.println ("\niterator:" + _time_iterator + "\nfor:" + _time_for + "\nforeach:" +_time_foreach); }}

The result: it may be because of the internal execution of Java, the different order caused by the length of the various, here collected a little data samples.

(Speed, sequencing, duration)

#2 for:354 milliseconds

#1 foreach:263 milliseconds

#3 iterator:425 milliseconds

Summary: A for after foreach adds speed.

=

#1 for:285 milliseconds

#2 iterator:301 milliseconds

#3 foreach:363 milliseconds

Summary: For the first appearance also got the fastest speed.

===

#3 foreach:517 milliseconds

#1 for:285 milliseconds

#2 iterator:478 milliseconds

Summary: foreach first appearance is not fast, the for speed is the same as the first appearance speed.

=

#3 foreach:416 milliseconds

#1 iterator:317 milliseconds

#2 for:321 milliseconds

Summary: Even when the iterator get the fastest speed, for there is almost no difference in speed.

===

#2 iterator:396 milliseconds

#3 foreach:415 milliseconds

#1 for:253 milliseconds

Summary: For the last appearance of the time to get the fastest speed.

===

#3 iterator:417 milliseconds
#1 for:332 milliseconds
#2 foreach:334 milliseconds

Summary: For the best speed.

Summary: Iterator and foreach do not increase the speed of the iteration. So you should use them for the necessary reasons, or the default for is faster.

Java test: Iterator foreach for three iterations in what way is it faster?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.