Skip to content

Commit 9ab9931

Browse files
author
Nicolai Parlog
committed
[06] Create benchmark for recreation/reuse of reg ex patterns
1 parent 350f543 commit 9ab9931

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ local.properties
2323
# Maven
2424

2525
target/
26+
dependency-reduced-pom.xml

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Effective Java
22

3-
This project contains examples for my [YouTube series on _Effective Java, Third Edition_](https://www.youtube.com/playlist?list=PL_-IO8LOLuNqUzvXfRCWRRJBswKEbLhgN).
3+
This project contains examples and benchmarks for my [YouTube series on _Effective Java, Third Edition_](https://www.youtube.com/playlist?list=PL_-IO8LOLuNqUzvXfRCWRRJBswKEbLhgN).
4+
To run the benchmarks:
5+
6+
```
7+
mvn package
8+
java -jar target/benchmarks.jar
9+
```
410

511
Related links:
612

@@ -21,3 +27,5 @@ Related links:
2127
* _Enforce the singleton property with a private constructor or an enum type_
2228
* _Enforce noninstantiability with a private constructor_
2329
* _Prefer dependency injection to hardwiring resources_
30+
* Item 6: _Avoid creating unnecessary objects_ -
31+
[benchmark](src/main/java/org/codefx/demo/effective_java/_06_unnecessary_objects/StringMatches.java)

pom.xml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,30 @@
1111
<properties>
1212
<maven.compiler.source>10</maven.compiler.source>
1313
<maven.compiler.target>10</maven.compiler.target>
14+
<jmh-version>1.21</jmh-version>
15+
<uberjar-name>benchmarks</uberjar-name>
1416
</properties>
1517

1618
<dependencies>
19+
<!-- main -->
1720
<dependency>
1821
<groupId>com.google.guava</groupId>
1922
<artifactId>guava</artifactId>
2023
<version>25.0-jre</version>
2124
</dependency>
25+
26+
<!-- jmh -->
27+
<dependency>
28+
<groupId>org.openjdk.jmh</groupId>
29+
<artifactId>jmh-core</artifactId>
30+
<version>${jmh-version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.openjdk.jmh</groupId>
34+
<artifactId>jmh-generator-annprocess</artifactId>
35+
<version>${jmh-version}</version>
36+
<scope>provided</scope>
37+
</dependency>
2238
</dependencies>
2339

2440
<build>
@@ -27,7 +43,82 @@
2743
<artifactId>maven-compiler-plugin</artifactId>
2844
<version>3.8.0</version>
2945
</plugin>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-shade-plugin</artifactId>
49+
<version>2.2</version>
50+
<executions>
51+
<execution>
52+
<phase>package</phase>
53+
<goals>
54+
<goal>shade</goal>
55+
</goals>
56+
<configuration>
57+
<finalName>${uberjar-name}</finalName>
58+
<transformers>
59+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
60+
<mainClass>org.openjdk.jmh.Main</mainClass>
61+
</transformer>
62+
</transformers>
63+
<filters>
64+
<filter>
65+
<!--
66+
Shading signed JARs will fail without this.
67+
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
68+
-->
69+
<artifact>*:*</artifact>
70+
<excludes>
71+
<exclude>META-INF/*.SF</exclude>
72+
<exclude>META-INF/*.DSA</exclude>
73+
<exclude>META-INF/*.RSA</exclude>
74+
</excludes>
75+
</filter>
76+
</filters>
77+
</configuration>
78+
</execution>
79+
</executions>
80+
</plugin>
3081
</plugins>
82+
<pluginManagement>
83+
<plugins>
84+
<plugin>
85+
<artifactId>maven-clean-plugin</artifactId>
86+
<version>2.5</version>
87+
</plugin>
88+
<plugin>
89+
<artifactId>maven-deploy-plugin</artifactId>
90+
<version>2.8.1</version>
91+
</plugin>
92+
<plugin>
93+
<artifactId>maven-install-plugin</artifactId>
94+
<version>2.5.1</version>
95+
</plugin>
96+
<plugin>
97+
<artifactId>maven-jar-plugin</artifactId>
98+
<version>2.4</version>
99+
</plugin>
100+
<plugin>
101+
<artifactId>maven-javadoc-plugin</artifactId>
102+
<version>2.9.1</version>
103+
</plugin>
104+
<plugin>
105+
<artifactId>maven-resources-plugin</artifactId>
106+
<version>2.6</version>
107+
</plugin>
108+
<plugin>
109+
<artifactId>maven-site-plugin</artifactId>
110+
<version>3.3</version>
111+
</plugin>
112+
<plugin>
113+
<artifactId>maven-source-plugin</artifactId>
114+
<version>2.2.1</version>
115+
</plugin>
116+
<plugin>
117+
<artifactId>maven-surefire-plugin</artifactId>
118+
<version>2.22.2</version>
119+
</plugin>
120+
</plugins>
121+
</pluginManagement>
31122
</build>
32123

33124
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.codefx.demo.effective_java._06_unnecessary_objects;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.BenchmarkMode;
5+
import org.openjdk.jmh.annotations.Fork;
6+
import org.openjdk.jmh.annotations.Measurement;
7+
import org.openjdk.jmh.annotations.Mode;
8+
import org.openjdk.jmh.annotations.OutputTimeUnit;
9+
import org.openjdk.jmh.annotations.Param;
10+
import org.openjdk.jmh.annotations.Scope;
11+
import org.openjdk.jmh.annotations.State;
12+
import org.openjdk.jmh.annotations.Warmup;
13+
14+
import java.util.concurrent.TimeUnit;
15+
import java.util.regex.Pattern;
16+
17+
@Fork(value = 3)
18+
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
19+
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
20+
@BenchmarkMode(Mode.AverageTime)
21+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
22+
@State(Scope.Thread)
23+
public class StringMatches {
24+
25+
private static final String ROMAN_REG_EX = "^(?=.)M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$";
26+
private static final Pattern ROMAN_PATTERN = Pattern.compile(ROMAN_REG_EX);
27+
28+
@Param({ "I", "XXXIV", "_" })
29+
private String numeral = "I";
30+
31+
@Benchmark
32+
public boolean recreatePatternOnEachMatch() {
33+
return numeral.matches(ROMAN_REG_EX);
34+
}
35+
36+
@Benchmark
37+
public boolean reusePattern() {
38+
return ROMAN_PATTERN.matcher(numeral).matches();
39+
}
40+
41+
}

0 commit comments

Comments
 (0)