Skip to content

Commit 4aa59ab

Browse files
author
Nicolai Parlog
committed
[06] Create benchmark for Exception::new
1 parent d7a1881 commit 4aa59ab

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.codefx.demo.effective_java._06_unnecessary_objects;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class Exceptions extends ObjectCreationBenchmarks {
10+
11+
@Benchmark
12+
public Exception createException() {
13+
// NOTE expensive operation:
14+
// Exceptions capture the entire call stack, which takes some time.
15+
return new IllegalArgumentException();
16+
}
17+
18+
@Benchmark
19+
public Map<String, Integer> createMap() {
20+
var map = new HashMap<String, Integer>();
21+
map.put("1", 1);
22+
map.put("2", 1);
23+
map.put("3", 1);
24+
return map;
25+
}
26+
27+
}

src/main/java/org/codefx/demo/effective_java/_06_unnecessary_objects/UrlEquals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class UrlEquals extends ObjectCreationBenchmarks {
1414

1515
@Benchmark
1616
public boolean urlEqual() throws MalformedURLException {
17-
// NOTE unnecessary object:
17+
// NOTE expensive operation:
1818
// There isn't anything unnecessary here. It's just that URL::equals
1919
// resolves the host name, which can take a while...
2020
return new URL(blog).equals(new URL(youtube));

0 commit comments

Comments
 (0)