Created
October 15, 2021 09:26
-
-
Save hkupty/d2d7de79b31e5f29e35cd28fa64c83a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package benchmarks.regex; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.regex.Pattern; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
public class ReplaceBenchmark { | |
@State(Scope.Benchmark) | |
public static class InputClass { | |
public List<String> testList; | |
@Setup(Level.Trial) | |
public void setUp() { | |
testList = new ArrayList<>( | |
Arrays.asList( | |
"HMHX-5I16-O6MU-N63O-YG8G-AOF2-756C", | |
"LAHR-PX03-DID4-EKKB-BJ7P-6U0D-UDCT", | |
"V0OS-SSUE-HLVY-IP4X-1QCN-0V79-2K7Q", | |
"712T-JGAV-1DGU-GJS9-EOI0-4KXJ-10FX", | |
"2OFJ-VZ9O-O80I-4ZX0-SQ6O-NVBB-IP0Z", | |
"5ZPZ-U3DJ-1CWK-YZEK-A0RF-JLW6-N0GW", | |
"1FQB-O06U-XFHP-TK3U-RKF5-KA7N-NV2I", | |
"UL0R-87OD-VN87-MW31-NZ87-CGMM-3GHJ", | |
"SQ94-CUG0-I73S-K8XX-2EOH-0X6S-VIDB", | |
"4EZC-S0Y5-NPZ6-GBEO-1G9U-T3D0-8VYM", | |
"Z3H5-Q42K-0U4W-TCBU-H3VL-5B4Y-A1R9", | |
"UYFK-I56S-APWC-7DA7-NJMH-I9MK-KA9X", | |
"V2X3-XMK3-CT2Q-TLLZ-KZHH-ENNN-8CVI", | |
"LQFL-T3VO-QOLJ-R5KZ-U6HO-XFUN-DP88", | |
"CEDZ-XH24-WERJ-QHMS-T3GS-8KZP-AI7J", | |
"7R14-7D6U-SWXX-XI06-PCJQ-1USM-LJ9L", | |
"QW0D-YDE2-K9JP-8U5A-MZ5L-7DZG-I5XE", | |
"PYFQ-74GS-UNJR-STA1-W7IY-TM7G-ADME", | |
"W9X4-8717-2W0O-X448-2I50-RLV0-W064", | |
"C0YU-2U5J-QYKP-CRXR-9ARH-2HM7-A225", | |
"D2V4-BGUM-R657-YACZ-QTAX-4167-S3RZ", | |
"WIS1-T3W1-6UT7-GRPR-9YQB-HS38-JN84", | |
"LEMN-CUV0-YUZ5-LJ7T-R15F-FSHA-PFPW", | |
"8QG4-M6K8-UVVY-74JY-T3WX-PB3Q-LODW", | |
"XYHP-JRP0-9X4B-20QJ-5CFD-UWZ5-J56N", | |
"IG1U-UZTH-TF5B-39UA-GKVL-LIYN-YSY0", | |
"NO7Q-1771-DF11-WRDG-XMLJ-04ET-LFA4", | |
"B5YA-VKJP-XUZE-U264-YX0Z-5429-Z3LN", | |
"W2UL-WWEJ-UUK6-RWJY-LTBV-LU7A-CJGA", | |
"1SQH-OD5O-O9RR-Q8QQ-7H13-RSPC-3MRH", | |
"1VE9-RH25-92WI-47OC-7NI5-KLM5-97SB", | |
"XF7F-VWBY-3U3E-GZ9T-RM6H-QXUW-EPX5", | |
"1SSC-QLA4-X2R0-9K00-SCCW-8FEG-WUMY", | |
"OHQW-P968-50OC-9I9Q-WAJY-AKZB-C2AF", | |
"7CPS-TUQA-67QG-AMWI-TL8A-ISXN-B7XI", | |
"N8RK-03WN-G9QS-Q4N2-9DKO-FG74-WB5T", | |
"P0YM-ZUX2-R4C0-V72B-FPYP-W7P3-9VCK", | |
"7BHB-9KGY-G9RQ-G03I-9147-FI63-WINN", | |
"9D3N-WBAC-7SC6-OQUT-AV1R-T1EM-TM8E", | |
"3LSU-6P9F-93VU-CYX8-35GS-O6XP-9JFY", | |
"RUQ7-0NWR-FC57-X2W5-ZXA3-Q1ZR-H7S7", | |
"KODQ-OW4K-OSIK-MG59-QWU0-FIT6-6NCV", | |
"E7HN-1YGO-O3N9-PEP4-1ZBN-YJRM-O70N", | |
"LMW1-TOJ6-FH0W-J23H-HOXO-J1GJ-I8A6", | |
"83G4-BIIZ-JRDP-K3JJ-KXXN-YM86-KHCD", | |
"R3UG-A16R-GXVF-92GN-ZY3H-YGVD-5YTH", | |
"LV5N-6J9S-457A-VM0K-UK4K-YCXJ-8QDV", | |
"GKL3-KJ8Q-MFG8-SI8M-A18U-2267-QNDM", | |
"60QN-U57W-W105-N3FW-XZTF-QZ3X-PGBU", | |
"2QYB-NN13-EX7D-FLDQ-6PMU-1IJ6-COQX")); | |
} | |
public List<String> getTestList() { | |
return testList; | |
} | |
} | |
public static final Pattern REPLACE_PATTERN = Pattern.compile("_"); | |
private String replaceWithStringReplace(String string) { | |
return string.replace("_", ""); | |
} | |
private String replaceWithPattern(String string) { | |
return REPLACE_PATTERN.matcher(string).replaceAll(""); | |
} | |
@Benchmark | |
@Fork(value = 1) | |
@Warmup(iterations = 2) | |
@Measurement(iterations = 2) | |
public void usingStringReplace(InputClass inputClass, Blackhole bh) { | |
for (String rs : inputClass.getTestList()) { | |
bh.consume(replaceWithStringReplace(rs)); | |
} | |
} | |
@Benchmark | |
@Fork(value = 1) | |
@Warmup(iterations = 2) | |
@Measurement(iterations = 2) | |
public void usingCompiledPattern(InputClass inputClass, Blackhole bh) { | |
for (String rs : inputClass.getTestList()) { | |
bh.consume(replaceWithPattern(rs)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Benchmark Mode Cnt Score Error Units | |
ReplaceBenchmark.usingCompiledPattern thrpt 2 143398.004 ops/s | |
ReplaceBenchmark.usingCompiledPattern:·gc.alloc.rate thrpt 2 1302.393 MB/sec | |
ReplaceBenchmark.usingCompiledPattern:·gc.alloc.rate.norm thrpt 2 10001.038 B/op | |
ReplaceBenchmark.usingCompiledPattern:·gc.churn.G1_Eden_Space thrpt 2 1306.088 MB/sec | |
ReplaceBenchmark.usingCompiledPattern:·gc.churn.G1_Eden_Space.norm thrpt 2 10029.260 B/op | |
ReplaceBenchmark.usingCompiledPattern:·gc.churn.G1_Survivor_Space thrpt 2 0.003 MB/sec | |
ReplaceBenchmark.usingCompiledPattern:·gc.churn.G1_Survivor_Space.norm thrpt 2 0.022 B/op | |
ReplaceBenchmark.usingCompiledPattern:·gc.count thrpt 2 127.000 counts | |
ReplaceBenchmark.usingCompiledPattern:·gc.time thrpt 2 54.000 ms | |
ReplaceBenchmark.usingStringReplace thrpt 2 1620601.122 ops/s | |
ReplaceBenchmark.usingStringReplace:·gc.alloc.rate thrpt 2 ≈ 10⁻⁴ MB/sec | |
ReplaceBenchmark.usingStringReplace:·gc.alloc.rate.norm thrpt 2 ≈ 10⁻⁵ B/op | |
ReplaceBenchmark.usingStringReplace:·gc.count thrpt 2 ≈ 0 counts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment