fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. class Ideone {
  7.  
  8. private static class Log {
  9.  
  10. public static void logLoopStats(String name, long elapsedNano, int charCount) {
  11. float timePerChar = (float) elapsedNano / (float) charCount;
  12. String part1 = String.format("%s: %,dns, character count: %,d. ", name, elapsedNano, charCount);
  13. String part2 = String.format("Time per character is %.3fns.", timePerChar);
  14. System.out.println(part1 + part2);
  15. }
  16.  
  17. }
  18.  
  19. public static void main(String[] args) throws Exception {
  20. List<String> list = new ArrayList<>();
  21.  
  22. for (int j = 0; j < 1000000; j++) {
  23. list.add("one");
  24. list.add("two");
  25. list.add("three");
  26. list.add("four");
  27. list.add("five");
  28. }
  29.  
  30. long startTime1 = System.nanoTime();
  31. int count1 = loopWithI(list);
  32. long elapsed1 = System.nanoTime() - startTime1;
  33.  
  34. long startTime2 = System.nanoTime();
  35. int count2 = loopWithISize(list);
  36. long elapsed2 = System.nanoTime() - startTime2;
  37.  
  38. long startTime3 = System.nanoTime();
  39. int count3 = loopWithIterator(list);
  40. long elapsed3 = System.nanoTime() - startTime3;
  41.  
  42. long startTime4 = System.nanoTime();
  43. int count4 = loopWithListIterator(list);
  44. long elapsed4 = System.nanoTime() - startTime4;
  45.  
  46. Log.logLoopStats("Loop with I, counting size", elapsed1, count1);
  47. Log.logLoopStats("Loop with I, not counting size", elapsed2, count2);
  48. Log.logLoopStats("Loop with iterator", elapsed3, count3);
  49. Log.logLoopStats("Loop with list iterator", elapsed4, count4);
  50. }
  51.  
  52. private static int loopWithI(List<String> list) {
  53. int i = 0;
  54. int charCount = 0;
  55. while (i < list.size()) {
  56. charCount += list.get(i).length();
  57. i++;
  58. }
  59. return charCount;
  60. }
  61.  
  62. private static int loopWithISize(List<String> list) {
  63. int i = 0;
  64. int charCount = 0;
  65. int size = list.size();
  66. while (i < size) {
  67. charCount += list.get(i).length();
  68. i++;
  69. }
  70. return charCount;
  71. }
  72.  
  73. private static int loopWithIterator(List<String> list) {
  74. Iterator<String> iterator = list.iterator();
  75. int charCount = 0;
  76. while (iterator.hasNext()) {
  77. charCount += iterator.next().length();
  78. }
  79. return charCount;
  80. }
  81.  
  82. private static int loopWithListIterator(List<String> list) {
  83. ListIterator<String> listIterator = list.listIterator();
  84. int charCount = 0;
  85. while (listIterator.hasNext()) {
  86. charCount += listIterator.next().length();
  87. }
  88. return charCount;
  89. }
  90.  
  91. }
Success #stdin #stdout 0.7s 320512KB
stdin
Standard input is empty
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.