Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 7b93ad9

Browse files
author
Bruce Eckel
committed
Non-working MapPerformanceJMH.java
To try to figure out how to get it to work.
1 parent 8c04f57 commit 7b93ad9

2 files changed

Lines changed: 91 additions & 34 deletions

File tree

threads/HorseRace.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public String tracks() {
4242
}
4343

4444
public class HorseRace {
45-
static final int FINISH_LINE = 75;
45+
static final int FINISH_LINE = 60;
4646
private List<Horse> horses = new ArrayList<>();
4747
private ExecutorService exec =
4848
Executors.newCachedThreadPool();
@@ -90,7 +90,7 @@ public static void main(String[] args) {
9090
}
9191
/* Output: (First and Last 18 Lines)
9292
===========================================================
93-
================
93+
=
9494
0
9595
**1
9696
2
@@ -99,47 +99,38 @@ public static void main(String[] args) {
9999
*5
100100
**6
101101
===========================================================
102-
================
102+
=
103103
**0
104104
***1
105-
**2
106-
**3
107-
***4
108-
*5
105+
*2
106+
*3
107+
****4
108+
**5
109109
****6
110110
===========================================================
111-
================
111+
=
112112
**0
113113
...________...________...________...________...
114-
***********************************************************
115-
*******6
114+
**********************************************6
116115
===========================================================
117-
================
118-
***********************************************************
119-
**********0
120-
***********************************************************
121-
**********1
122-
***********************************************************
123-
***********2
116+
=
117+
**************************************************0
118+
*************************************************1
119+
*********************************************2
124120
***********************************************************
125-
***************3
126-
********************************************************4
127-
*********************************************************5
128-
***********************************************************
129-
*******6
121+
3
122+
*****************************************************4
123+
*****************************************************5
124+
***********************************************6
130125
===========================================================
131-
================
132-
***********************************************************
133-
***********0
134-
***********************************************************
135-
**********1
126+
=
127+
****************************************************0
128+
*************************************************1
129+
***********************************************2
136130
***********************************************************
137-
************2
138-
***********************************************************
139-
*****************3
140-
*********************************************************4
141-
**********************************************************5
142-
***********************************************************
143-
*******6
131+
*3
132+
*******************************************************4
133+
*******************************************************5
134+
***********************************************6
144135
Horse 3 won!
145136
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// understandingcollections/jmhtests/MapPerformanceJMH.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// Demonstrates performance differences in Maps
6+
package understandingcollections.jmhtests;
7+
import org.openjdk.jmh.annotations.*;
8+
import org.openjdk.jmh.infra.Blackhole;
9+
import java.util.*;
10+
11+
@State(Scope.Thread)
12+
public class MapPerformanceJMH {
13+
private Map<Integer, Integer> map;
14+
15+
@Param({"hashmap", "treemap"})
16+
private String type;
17+
18+
private int begin;
19+
private int end;
20+
21+
@Setup
22+
public void setup() {
23+
if (type.equals("hashmap")) {
24+
map = new HashMap<Integer, Integer>();
25+
} else if (type.equals("treemap")) {
26+
map = new TreeMap<Integer, Integer>();
27+
} else {
28+
throw new IllegalStateException("Unknown type: " + type);
29+
}
30+
31+
begin = 1;
32+
end = 256;
33+
for (int i = begin; i < end; i++) {
34+
map.put(i, i);
35+
}
36+
}
37+
38+
@Benchmark
39+
public void get(Blackhole bh) {
40+
for (int i = begin; i < end; i++) {
41+
bh.consume(map.get(i));
42+
}
43+
}
44+
45+
// These are incomplete placeholders from the
46+
// original MapPerformance.jaava:
47+
/* @Benchmark
48+
int put() {
49+
for(int i = 0; i < loops; i++) {
50+
map.clear();
51+
for(int j = 0; j < size; j++)
52+
map.put(j, j);
53+
}
54+
return loops * size;
55+
}*/
56+
57+
/* @Benchmark
58+
int iterate() {
59+
for(int i = 0; i < loops; i ++) {
60+
Iterator it = map.entrySet().iterator();
61+
while(it.hasNext())
62+
it.next();
63+
}
64+
}*/
65+
66+
}

0 commit comments

Comments
 (0)