import java.util.* ;
import java.lang.* ;
import java.io.* ;
class Ideone {
private static class Log {
public static void logLoopStats
( String name,
long elapsedNano,
int charCount
) { float timePerChar = ( float ) elapsedNano / ( float ) charCount;
String part1
= String .
format ( "%s: %,dns, character count: %,d. " , name, elapsedNano, charCount
) ; String part2
= String .
format ( "Time per character is %.3fns." , timePerChar
) ; System .
out .
println ( part1
+ part2
) ; }
}
List< String> list = new ArrayList<> ( ) ;
for ( int j = 0 ; j < 1000000 ; j++ ) {
list.add ( "one" ) ;
list.add ( "two" ) ;
list.add ( "three" ) ;
list.add ( "four" ) ;
list.add ( "five" ) ;
}
long startTime1
= System .
nanoTime ( ) ; int count1 = loopWithI( list) ;
long elapsed1
= System .
nanoTime ( ) - startTime1
;
long startTime2
= System .
nanoTime ( ) ; int count2 = loopWithISize( list) ;
long elapsed2
= System .
nanoTime ( ) - startTime2
;
long startTime3
= System .
nanoTime ( ) ; int count3 = loopWithIterator( list) ;
long elapsed3
= System .
nanoTime ( ) - startTime3
;
long startTime4
= System .
nanoTime ( ) ; int count4 = loopWithListIterator( list) ;
long elapsed4
= System .
nanoTime ( ) - startTime4
;
Log.logLoopStats ( "Loop with I, counting size" , elapsed1, count1) ;
Log.logLoopStats ( "Loop with I, not counting size" , elapsed2, count2) ;
Log.logLoopStats ( "Loop with iterator" , elapsed3, count3) ;
Log.logLoopStats ( "Loop with list iterator" , elapsed4, count4) ;
}
private static int loopWithI( List< String> list) {
int i = 0 ;
int charCount = 0 ;
while ( i < list.size ( ) ) {
charCount += list.get ( i) .length ( ) ;
i++;
}
return charCount;
}
private static int loopWithISize( List< String> list) {
int i = 0 ;
int charCount = 0 ;
int size = list.size ( ) ;
while ( i < size) {
charCount += list.get ( i) .length ( ) ;
i++;
}
return charCount;
}
private static int loopWithIterator( List< String> list) {
Iterator< String> iterator = list.iterator ( ) ;
int charCount = 0 ;
while ( iterator.hasNext ( ) ) {
charCount += iterator.next ( ) .length ( ) ;
}
return charCount;
}
private static int loopWithListIterator( List< String> list) {
ListIterator< String> listIterator = list.listIterator ( ) ;
int charCount = 0 ;
while ( listIterator.hasNext ( ) ) {
charCount += listIterator.next ( ) .length ( ) ;
}
return charCount;
}
}
CmltcG9ydCBqYXZhLnV0aWwuKjsKaW1wb3J0IGphdmEubGFuZy4qOwppbXBvcnQgamF2YS5pby4qOwoKY2xhc3MgSWRlb25lIHsKCQoJcHJpdmF0ZSBzdGF0aWMgY2xhc3MgTG9nIHsKCgkgICAgcHVibGljIHN0YXRpYyB2b2lkIGxvZ0xvb3BTdGF0cyhTdHJpbmcgbmFtZSwgbG9uZyBlbGFwc2VkTmFubywgaW50IGNoYXJDb3VudCkgewoJICAgICAgICBmbG9hdCB0aW1lUGVyQ2hhciA9IChmbG9hdCkgZWxhcHNlZE5hbm8gLyAoZmxvYXQpIGNoYXJDb3VudDsKCSAgICAgICAgU3RyaW5nIHBhcnQxID0gU3RyaW5nLmZvcm1hdCgiJXM6ICUsZG5zLCBjaGFyYWN0ZXIgY291bnQ6ICUsZC4gIiwgbmFtZSwgZWxhcHNlZE5hbm8sIGNoYXJDb3VudCk7CgkgICAgICAgIFN0cmluZyBwYXJ0MiA9IFN0cmluZy5mb3JtYXQoIlRpbWUgcGVyIGNoYXJhY3RlciBpcyAlLjNmbnMuIiwgdGltZVBlckNoYXIpOwoJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocGFydDEgKyBwYXJ0Mik7CgkgICAgfQoKCX0KCQoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIEV4Y2VwdGlvbiB7CiAgICAgICAgTGlzdDxTdHJpbmc+IGxpc3QgPSBuZXcgQXJyYXlMaXN0PD4oKTsKCiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCAxMDAwMDAwOyBqKyspIHsKICAgICAgICAgICAgbGlzdC5hZGQoIm9uZSIpOwogICAgICAgICAgICBsaXN0LmFkZCgidHdvIik7CiAgICAgICAgICAgIGxpc3QuYWRkK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KCJMb29wIHdpdGggaXRlcmF0b3IiLCBlbGFwc2VkMywgY291bnQzKTsKICAgICAgICBMb2cubG9nTG9vcFN0YXRzKCJMb29wIHdpdGggbGlzdCBpdGVyYXRvciIsIGVsYXBzZWQ0LCBjb3VudDQpOwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGludCBsb29wV2l0aEkoTGlzdDxTdHJpbmc+IGxpc3QpIHsKICAgICAgICBpbnQgaSA9IDA7CiAgICAgICAgaW50IGNoYXJDb3VudCA9IDA7CiAgICAgICAgd2hpbGUgKGkgPCBsaXN0LnNpemUoKSkgewogICAgICAgICAgICBjaGFyQ291bnQgKz0gbGlzdC5nZXQoaSkubGVuZ3RoKCk7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNoYXJDb3VudDsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyBpbnQgbG9vcFdpdGhJU2l6ZShMaXN0PFN0cmluZz4gbGlzdCkgewogICAgICAgIGludCBpID0gMDsKICAgICAgICBpbnQgY2hhckNvdW50ID0gMDsKICAgICAgICBpbnQgc2l6ZSA9IGxpc3Quc2l6ZSgpOwogICAgICAgIHdoaWxlIChpIDwgc2l6ZSkgewogICAgICAgICAgICBjaGFyQ291bnQgKz0gbGlzdC5nZXQoaSkubGVuZ3RoKCk7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNoYXJDb3VudDsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyBpbnQgbG9vcFdpdGhJdGVyYXRvcihMaXN0PFN0cmluZz4gbGlzdCkgewogICAgICAgIEl0ZXJhdG9yPFN0cmluZz4gaXRlcmF0b3IgPSBsaXN0Lml0ZXJhdG9yKCk7CiAgICAgICAgaW50IGNoYXJDb3VudCA9IDA7CiAgICAgICAgd2hpbGUgKGl0ZXJhdG9yLmhhc05leHQoKSkgewogICAgICAgICAgICBjaGFyQ291bnQgKz0gaXRlcmF0b3IubmV4dCgpLmxlbmd0aCgpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY2hhckNvdW50OwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGludCBsb29wV2l0aExpc3RJdGVyYXRvcihMaXN0PFN0cmluZz4gbGlzdCkgewogICAgICAgIExpc3RJdGVyYXRvcjxTdHJpbmc+IGxpc3RJdGVyYXRvciA9IGxpc3QubGlzdEl0ZXJhdG9yKCk7CiAgICAgICAgaW50IGNoYXJDb3VudCA9IDA7CiAgICAgICAgd2hpbGUgKGxpc3RJdGVyYXRvci5oYXNOZXh0KCkpIHsKICAgICAgICAgICAgY2hhckNvdW50ICs9IGxpc3RJdGVyYXRvci5uZXh0KCkubGVuZ3RoKCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBjaGFyQ291bnQ7CiAgICB9CiAgICAKfQ==
stdout
Loop with I, counting size: 95,307,356ns, character count: 19,000,000. Time per character is 5.016ns.
Loop with I, not counting size: 59,286,751ns, character count: 19,000,000. Time per character is 3.120ns.
Loop with iterator: 86,909,725ns, character count: 19,000,000. Time per character is 4.574ns.
Loop with list iterator: 85,995,007ns, character count: 19,000,000. Time per character is 4.526ns.