1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package org.openjdk.micro.jdk.java.util.stream; |
|
26 |
|
27 import java.util.ArrayList; |
|
28 import java.util.Arrays; |
|
29 import java.util.HashMap; |
|
30 import java.util.HashSet; |
|
31 import java.util.Random; |
|
32 import java.util.Set; |
|
33 import java.util.concurrent.ConcurrentHashMap; |
|
34 import java.util.function.BinaryOperator; |
|
35 import org.openjdk.jmh.annotations.Benchmark; |
|
36 import org.openjdk.jmh.annotations.Param; |
|
37 import org.openjdk.jmh.annotations.Scope; |
|
38 import org.openjdk.jmh.annotations.Setup; |
|
39 import org.openjdk.jmh.annotations.State; |
|
40 |
|
41 /** |
|
42 * JMH Benchmark created to mimic a JSR-166 benchmark |
|
43 */ |
|
44 @State(Scope.Benchmark) |
|
45 public class IntegerSum { |
|
46 |
|
47 @Param({"1", "10", "100", "1000", "10000", "100000", "1000000"}) |
|
48 private int size; |
|
49 |
|
50 private ArrayList<Integer> arrayList; |
|
51 private HashMap<Integer, Integer> hashMap; |
|
52 private ConcurrentHashMap<Integer, Integer> concurrentHashMap; |
|
53 |
|
54 private static final BinaryOperator<Integer> sum = (x, y) -> x + y; |
|
55 |
|
56 @Setup |
|
57 public final void setup() { |
|
58 Random srand = new Random(9820239874L); |
|
59 Set<Integer> set = new HashSet<>(size); |
|
60 while (set.size() < size) { |
|
61 set.add(srand.nextInt()); |
|
62 } |
|
63 Integer[] values = set.toArray(new Integer[size]); |
|
64 Integer[] keys = Arrays.copyOf(values, size); |
|
65 |
|
66 for (int i = 0; i < size; i++) { |
|
67 swap(values, i, srand.nextInt(values.length)); |
|
68 swap(keys, i, srand.nextInt(keys.length)); |
|
69 } |
|
70 |
|
71 arrayList = new ArrayList<>(Arrays.asList(keys)); |
|
72 hashMap = new HashMap<>(size); |
|
73 concurrentHashMap = new ConcurrentHashMap<>(size); |
|
74 for (int i = 0; i < size; i++) { |
|
75 hashMap.put(keys[i], values[i]); |
|
76 concurrentHashMap.put(keys[i], values[i]); |
|
77 } |
|
78 |
|
79 System.gc(); |
|
80 } |
|
81 |
|
82 private void swap(Integer[] array, int first, int second) { |
|
83 Integer temp = array[first]; |
|
84 array[first] = array[second]; |
|
85 array[second] = temp; |
|
86 } |
|
87 |
|
88 @Benchmark |
|
89 public Integer ArrayListStream() { |
|
90 return arrayList.stream().reduce(0, sum); |
|
91 } |
|
92 |
|
93 @Benchmark |
|
94 public Integer HashMapStream() { |
|
95 return hashMap.keySet().stream().reduce(0, sum); |
|
96 } |
|
97 |
|
98 @Benchmark |
|
99 public Integer ConcurrentHashMapStream() { |
|
100 return concurrentHashMap.keySet().stream().reduce(0, sum); |
|
101 } |
|
102 |
|
103 @Benchmark |
|
104 public Integer ArrayListParallelStream() { |
|
105 return arrayList.parallelStream().reduce(0, sum); |
|
106 } |
|
107 |
|
108 @Benchmark |
|
109 public Integer HashMapParallelStream() { |
|
110 return hashMap.keySet().parallelStream().reduce(0, sum); |
|
111 } |
|
112 |
|
113 @Benchmark |
|
114 public Integer ConcurrentHashMapParallelStream() { |
|
115 return concurrentHashMap.keySet().parallelStream().reduce(0, sum); |
|
116 } |
|
117 } |
|