Skip to content

Commit

Permalink
add Pattern.split benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
sjamesr committed Jun 27, 2022
1 parent 5c06a9e commit 7ba33e2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 The Go Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package com.google.re2j.benchmark;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.TimeUnit;

@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class BenchmarkSplit {
@Param({"JDK", "RE2J"})
private Implementations impl;

private Implementations.Pattern pattern;

private String input;

@Setup
public void setup() {
pattern = Implementations.Pattern.compile(impl, "a");

StringBuilder b = new StringBuilder();
for (int i = 0; i < 100; i++) {
b.append("this should be a pretty big string containing a few delimiters to split on");
}

input = b.toString();
}

@Benchmark
public void benchmarkSplit(Blackhole bh) {
bh.consume(pattern.split(input));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public static Pattern compile(Implementations impl, String pattern) {

public abstract Matcher matcher(byte[] bytes);

public abstract String[] split(String str);

public static class JdkPattern extends Pattern {

private final java.util.regex.Pattern pattern;
Expand All @@ -99,6 +101,11 @@ public Matcher matcher(String str) {
public Matcher matcher(byte[] bytes) {
return new Matcher.JdkMatcher(pattern.matcher(new String(bytes)));
}

@Override
public String[] split(String str) {
return pattern.split(str);
}
}

public static class Re2Pattern extends Pattern {
Expand All @@ -118,6 +125,11 @@ public Matcher matcher(String str) {
public Matcher matcher(byte[] bytes) {
return new Matcher.Re2Matcher(pattern.matcher(bytes));
}

@Override
public String[] split(String str) {
return pattern.split(str);
}
}
}
}

0 comments on commit 7ba33e2

Please sign in to comment.