java - 按正则表达式拆分与多个一个字符拆分性能
问题描述
我使用此基准比较了通过正则表达式和多个单字符拆分来拆分字符串
import org.openjdk.jmh.annotations.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Test {
static String start = "1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.1, 2, 3, 4, 5, 6, 7, 8. 9. 10. 11. 12.";
public static void main(String[] args) throws IOException {
org.openjdk.jmh.Main.main(args);
}
@Fork(value = 1, warmups = 0)
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 0)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void splitByRegex() {
String test = start;
test = String.join("_", test.split("[1,.]"));
}
@Fork(value = 1, warmups = 0)
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 0)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void multipleSplitByOneChar() {
String test = start;
test = String.join("_", test.split("\\."));
test = String.join("_", test.split(","));
test = String.join("_", test.split("1"));
}
}
并得到了这些结果
Benchmark Mode Cnt Score Error Units
Test.multipleSplitByOneChar avgt 5 10493,118 ± 572,528 ns/op
Test.splitByRegex avgt 5 15519,418 ± 913,220 ns/op
为什么用正则表达式分割比用多个单个字符分割要慢,即使它们产生相同的结果?
笔记:
- 我在 JDK 14.0.2 上运行代码
- 我用的是 JMH 1.28
解决方案
String.split
实现具有按一个字符分割的优化快速路径。
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
* (1) one-char String and this character is not one of the
* RegEx's meta characters ".$|()[{^?*+\\", or
* (2) two-char String and the first char is the backslash and
* the second is not the ascii digit or ascii letter.
*/
推荐阅读
- java - 按两个参数自定义排序无法正常工作
- r - sf multipolygon 错误:C 堆栈使用量太接近限制
- google-apps-script - 谷歌应用脚本根据依赖下拉列表粘贴行
- intellij-idea - IntelliJ 项目工具窗口的顶行是什么意思?
- php - 尽管其他列包含不同的值,但是否有隐藏一列中的重复项的功能?
- c# - ASP.NET - 类型或命名空间不存在
- asp.net - ASP.NET Core 网关 ocelot API 不起作用
- python - 无法显示 csv 文件的全部内容
- sql - WITH AS 语句后的 Bigquery INSERT 不起作用
- java - 使用 jOOQ 加载 PostgreSQL 树