FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/IterateThroughHashMapTest.java at main · Eggy115/Java · GitHub
Eggy115
/
Java
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5
Star
3
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
IterateThroughHashMapTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
173 lines (153 loc) · 5.66 KB
Breadcrumbs
Java
/
IterateThroughHashMapTest.java
Copy path
File metadata and controls
173 lines (153 loc) · 5.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package
other_examples
;
import
org
.
apache
.
commons
.
collections4
.
IterableMap
;
import
org
.
apache
.
commons
.
collections4
.
MapIterator
;
import
org
.
apache
.
commons
.
collections4
.
map
.
HashedMap
;
import
org
.
eclipse
.
collections
.
api
.
map
.
MutableMap
;
import
org
.
eclipse
.
collections
.
impl
.
map
.
mutable
.
UnifiedMap
;
import
org
.
openjdk
.
jmh
.
annotations
.*;
import
org
.
openjdk
.
jmh
.
runner
.
Runner
;
import
org
.
openjdk
.
jmh
.
runner
.
RunnerException
;
import
org
.
openjdk
.
jmh
.
runner
.
options
.
Options
;
import
org
.
openjdk
.
jmh
.
runner
.
options
.
OptionsBuilder
;
import
java
.
io
.
IOException
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Iterator
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
TimeUnit
;
import
java
.
util
.
stream
.
Collector
;
import
java
.
util
.
stream
.
Collectors
;
/**
* Created by vvedenin on 2/21/2016.
*/
@
State
(
Scope
.
Benchmark
)
public
class
IterateThroughHashMapTest
{
private
final
static
Integer
SIZE
=
100
;
@
Param
({
"100"
,
"1000"
})
public
int
size
;
private
Map
<
Integer
,
Integer
>
map
=
new
HashMap
<>(
SIZE
);
/** 1. Using iterator and Map.Entry **/
@
Benchmark
public
long
test1_UsingWhileAndMapEntry
()
throws
IOException
{
long
i
=
0
;
Iterator
<
Map
.
Entry
<
Integer
,
Integer
>>
it
=
map
.
entrySet
().
iterator
();
while
(
it
.
hasNext
()) {
Map
.
Entry
<
Integer
,
Integer
>
pair
=
it
.
next
();
i
+=
pair
.
getKey
() +
pair
.
getValue
();
}
return
i
;
}
/** 2. Using foreach and Map.Entry **/
@
Benchmark
public
long
test2_UsingForEachAndMapEntry
()
throws
IOException
{
long
i
=
0
;
for
(
Map
.
Entry
<
Integer
,
Integer
>
pair
:
map
.
entrySet
()) {
i
+=
pair
.
getKey
() +
pair
.
getValue
();
}
return
i
;
}
/** 3. Using foreach from Java 8 **/
@
Benchmark
public
long
test3_UsingForEachAndJava8
()
throws
IOException
{
final
long
[]
i
= {
0
};
map
.
forEach
((
k
,
v
) ->
i
[
0
] +=
k
+
v
);
return
i
[
0
];
}
/** 4. Using keySet and foreach **/
@
Benchmark
public
long
test4_UsingKeySetAndForEach
()
throws
IOException
{
long
i
=
0
;
for
(
Integer
key
:
map
.
keySet
()) {
i
+=
key
+
map
.
get
(
key
);
}
return
i
;
}
/** 5. Using keySet and iterator **/
@
Benchmark
public
long
test5_UsingKeySetAndIterator
()
throws
IOException
{
long
i
=
0
;
Iterator
<
Integer
>
itr2
=
map
.
keySet
().
iterator
();
while
(
itr2
.
hasNext
()) {
Integer
key
=
itr2
.
next
();
i
+=
key
+
map
.
get
(
key
);
}
return
i
;
}
/** 6. Using for and Map.Entry **/
@
Benchmark
public
long
test6_UsingForAndIterator
()
throws
IOException
{
long
i
=
0
;
for
(
Iterator
<
Map
.
Entry
<
Integer
,
Integer
>>
entries
=
map
.
entrySet
().
iterator
();
entries
.
hasNext
(); ) {
Map
.
Entry
<
Integer
,
Integer
>
entry
=
entries
.
next
();
i
+=
entry
.
getKey
() +
entry
.
getValue
();
}
return
i
;
}
/** 7. Using Java 8 Stream Api **/
@
Benchmark
public
long
test7_UsingJava8StreamApi
()
throws
IOException
{
final
long
[]
i
= {
0
};
map
.
entrySet
().
stream
().
forEach
(
e
->
i
[
0
] +=
e
.
getKey
() +
e
.
getValue
());
return
i
[
0
];
}
/** 8. Using Java 8 Stream Api parallel **/
@
Benchmark
public
long
test8_UsingJava8StreamApiParallel
()
throws
IOException
{
final
long
[]
i
= {
0
};
map
.
entrySet
().
stream
().
parallel
().
forEach
(
e
->
i
[
0
] +=
e
.
getKey
() +
e
.
getValue
());
return
i
[
0
];
}
/** 9. Using Apache IterableMap **/
private
IterableMap
<
Integer
,
Integer
>
iterableMap
=
new
HashedMap
<>(
SIZE
);
@
Benchmark
public
long
test9_UsingApacheIterableMap
()
throws
IOException
{
long
i
=
0
;
MapIterator
<
Integer
,
Integer
>
it
=
iterableMap
.
mapIterator
();
while
(
it
.
hasNext
()) {
i
+=
it
.
next
() +
it
.
getValue
();
}
return
i
;
}
/** 10. Using MutableMap of Eclipse (CS) collections **/
private
MutableMap
<
Integer
,
Integer
>
mutableMap
=
UnifiedMap
.
newMap
(
SIZE
);
@
Benchmark
public
long
test10_UsingEclipseMap
()
throws
IOException
{
final
long
[]
i
= {
0
};
mutableMap
.
forEachKeyValue
((
key
,
value
) -> {
i
[
0
] +=
key
+
value
;
});
return
i
[
0
];
}
/** 11. Using Java 8 Stream Api 2 **/
@
Benchmark
public
long
test11_UsingJava8StreamApi2
()
throws
IOException
{
return
map
.
entrySet
().
stream
().
mapToLong
(
e
->
e
.
getKey
() +
e
.
getValue
()).
sum
();
}
/** 12. Using Java 8 Stream Api parallel 2 **/
@
Benchmark
public
long
test12_UsingJava8StreamApiparallel2
()
throws
IOException
{
return
map
.
entrySet
().
parallelStream
().
mapToLong
(
e
->
e
.
getKey
() +
e
.
getValue
()).
sum
();
}
@
TearDown
(
Level
.
Iteration
)
public
void
tearDown
() {
map
=
new
HashMap
<>(
size
);
iterableMap
=
new
HashedMap
<>(
size
);
mutableMap
=
UnifiedMap
.
newMap
(
size
);
for
(
int
i
=
0
;
i
<
size
;
i
++) {
map
.
put
(
i
,
i
);
mutableMap
.
put
(
i
,
i
);
iterableMap
.
put
(
i
,
i
);
}
}
public
static
void
main
(
String
[]
args
)
throws
RunnerException
{
Options
opt
=
new
OptionsBuilder
()
.
include
(
IterateThroughHashMapTest
.
class
.
getSimpleName
())
.
timeUnit
(
TimeUnit
.
MICROSECONDS
)
.
warmupIterations
(
3
)
.
measurementIterations
(
5
)
.
param
(
"size"
,
"100"
,
/*"500","900","1300","1700","2100","2500","5000","10000","15000","20000","25000" ,*/
"30000"
)
.
forks
(
1
)
.
mode
(
Mode
.
AverageTime
)
.
build
();
new
Runner
(
opt
).
run
();
}
}
Back
|
FazBrowse Home
|
New Git URL