Skip to content

Commit d7a1881

Browse files
author
Nicolai Parlog
committed
[06] Create benchmark for URL::equals
1 parent 8105b4c commit d7a1881

File tree

1 file changed

+28
-0
lines changed
  • src/main/java/org/codefx/demo/effective_java/_06_unnecessary_objects

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.codefx.demo.effective_java._06_unnecessary_objects;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.Param;
5+
6+
import java.net.MalformedURLException;
7+
import java.net.URL;
8+
9+
public class UrlEquals extends ObjectCreationBenchmarks {
10+
11+
private String blog = "https://codefx.org";
12+
13+
private String youtube = "https://youtube.com/codefx";
14+
15+
@Benchmark
16+
public boolean urlEqual() throws MalformedURLException {
17+
// NOTE unnecessary object:
18+
// There isn't anything unnecessary here. It's just that URL::equals
19+
// resolves the host name, which can take a while...
20+
return new URL(blog).equals(new URL(youtube));
21+
}
22+
23+
@Benchmark
24+
public boolean stringEqual() throws MalformedURLException {
25+
return blog.equals(youtube);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)