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?