Guest User

Stream Loop Performance Test in Java

a guest
Jan 24th, 2017
1,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package dlh;
  2.  
  3. import java.time.Instant;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. /**
  8.  * Licence: Public domain.
  9.  * @author fuchs@hq.kwsoft.de
  10.  */
  11. public class StreamPerformance {
  12.    
  13.     public static void main(String[] args) {
  14.         StreamPerformance instance = new StreamPerformance();
  15.         List<Integer> amounts = new ArrayList<>();
  16.         for (int ii = 0; ii < 5; ii++) {
  17.             int amount = (int)Math.pow(10, ii);
  18.             amounts.add(amount);
  19.             instance.action(amount);
  20.         line(ITERABLE_FOR_EACH, amounts);
  21.         line(FOR_COLON_EACH, amounts);
  22.         line(ITERABLE_STREAM_FOR_EACH, amounts);
  23.         line(FOR_WITH_INDEX, amounts);
  24.         }
  25.        
  26.         String header = leftPad("", 26);
  27.         for(Integer next : amounts) {
  28.             header += leftPad("" + next, ("" + next).length() +GAP);
  29.         }
  30.         System.out.println(header);
  31.         line(ITERABLE_FOR_EACH, amounts);
  32.         line(FOR_COLON_EACH, amounts);
  33.         line(ITERABLE_STREAM_FOR_EACH, amounts);
  34.         line(FOR_WITH_INDEX, amounts);
  35.        
  36.         //                   10   100    1000   10000  100000
  37.         // iterator.forEach
  38.     }
  39.    
  40.     private final static int GAP = 4;
  41.    
  42.     private static void line(String which, List<Integer> amounts) {
  43.         System.out.print(leftPad(which, 26));
  44.         for(Integer next : amounts) {
  45.             for(Result nextR : RESULTS) {
  46.                 if(nextR.integerCount == next && nextR.method.equals(which)) {
  47.                     System.out.print(leftPad(nextR.millis + "", ("" + next).length() +GAP));
  48.                 }
  49.             }
  50.         }
  51.         System.out.println("");
  52.     }
  53.    
  54.     private static String leftPad(String input, int size) {
  55.         StringBuilder foo = new StringBuilder();
  56.         for(int ii = input.length(); ii < size; ii++) {
  57.             foo.append(" ");
  58.         }
  59.         foo.append(input);
  60.         return foo.toString();
  61.     }
  62.  
  63.     public static final String ITERABLE_FOR_EACH =        "iterator.forEach";
  64.     public static final String FOR_COLON_EACH =           "for:each";
  65.     public static final String ITERABLE_STREAM_FOR_EACH = "iterable.stream.forEach";
  66.     public static final String FOR_WITH_INDEX =           "for with index";
  67.    
  68.     private static class Result {
  69.        
  70.         int integerCount;
  71.         String method;
  72.         long millis;
  73.  
  74.         public Result(int integerCount, String method, long millis) {
  75.             this.integerCount = integerCount;
  76.             this.method = method;
  77.             this.millis = millis;
  78.         }
  79.     }
  80.    
  81.     private final static List<Result> RESULTS = new ArrayList<>();
  82.    
  83.     public void action(int integerCount) {
  84.         System.out.println("action: " + integerCount);
  85.         List<Integer> integers = new ArrayList<>(integerCount);
  86.         for(int ii = 0; ii < integerCount; ii++) {
  87.             integers.add(ii);
  88.             // calculate once to avoid penality for the first section.
  89.             outside = ii*ii;
  90.         }
  91.        
  92.         int iterations = 10_000_000;
  93.        
  94.         Instant beforeFor = Instant.now();
  95.         for(int ii = 0; ii < iterations; ii++) {
  96.             forCounter(integers);
  97.         }
  98.         Instant afterFor = Instant.now();
  99.         RESULTS.add(new Result(integerCount, FOR_WITH_INDEX, (afterFor.toEpochMilli() - beforeFor.toEpochMilli())));
  100.        
  101.         Instant beforeStream = Instant.now();
  102.         for(int ii = 0; ii < iterations; ii++) {
  103.             iteratorStream(integers);
  104.         }
  105.         Instant afterStream = Instant.now();
  106.         RESULTS.add(new Result(integerCount, ITERABLE_STREAM_FOR_EACH, (afterStream.toEpochMilli() - beforeStream.toEpochMilli())));
  107.        
  108.         Instant beforeForEach = Instant.now();
  109.         for(int ii = 0; ii < iterations; ii++) {
  110.             forEach(integers);
  111.         }
  112.         Instant afterForEach = Instant.now();
  113.         RESULTS.add(new Result(integerCount, FOR_COLON_EACH, (afterForEach.toEpochMilli() - beforeForEach.toEpochMilli())));
  114.        
  115.         Instant beforeIterator = Instant.now();
  116.         for(int ii = 0; ii < iterations; ii++) {
  117.             iteratorForEach(integers);
  118.         }
  119.         Instant afterIterator = Instant.now();
  120.         RESULTS.add(new Result(integerCount, ITERABLE_FOR_EACH, (afterIterator.toEpochMilli() - beforeIterator.toEpochMilli())));
  121.     }
  122.    
  123.     public int outside = 0;
  124.    
  125.     private void forCounter(List<Integer> integers) {
  126.         for(int ii = 0; ii < integers.size(); ii++) {
  127.             Integer next = integers.get(ii);
  128.             outside = next*next;
  129.         }
  130.     }
  131.    
  132.     private void forEach(List<Integer> integers) {
  133.         for(Integer next : integers) {
  134.             outside = next * next;
  135.         }
  136.     }
  137.    
  138.     private void iteratorForEach(List<Integer> integers) {
  139.         integers.forEach((ii) -> {
  140.             outside = ii*ii;
  141.         });
  142.     }
  143.     private void iteratorStream(List<Integer> integers) {
  144.         integers.stream().forEach((ii) -> {
  145.             outside = ii*ii;
  146.         });
  147.     }
  148. }
Add Comment
Please, Sign In to add comment