Skip to content

Instantly share code, notes, and snippets.

@ckampfe
Last active September 25, 2022 19:55
Show Gist options
  • Save ckampfe/dca8785ad1a19a9dde474537fb1e21b0 to your computer and use it in GitHub Desktop.
Save ckampfe/dca8785ad1a19a9dde474537fb1e21b0 to your computer and use it in GitHub Desktop.
regex_str = "[a-z]+\\s+\\d+"
small_hit_str = "abc 123 def 456"
big_hit_str = Enum.reduce(1..1000, "", fn _val, acc ->
"#{acc}#{small_hit_str}"
end)
small_miss_str = "abc abc abc abc"
big_miss_str = Enum.reduce(1..1000, "", fn _val, acc ->
"#{acc}#{small_miss_str}"
end)
elixir_regex = Regex.compile!(regex_str)
{:ok, rust_regex} = RegexRs.compile(regex_str)
{:ok, rust_named_capture_regex} = RegexRs.compile("(?P<foo>\\d+)")
elixir_named_capture_regex = Regex.compile!("(?<foo>\\d+)")
{:ok, rust_run_regex} = RegexRs.compile("[a-z]+\\s+\(\\d+\)")
elixir_run_regex = Regex.compile!("[a-z]+\\s+\(\\d+\)")
Benchee.run(
%{
"elixir scan" => fn i ->
Regex.scan(elixir_regex, i)
end,
"rust scan" => fn i ->
RegexRs.find_iter(rust_regex, i)
end,
"rust match" => fn i ->
RegexRs.is_match(rust_regex, i)
end,
"elixir match" => fn i ->
Regex.match?(elixir_regex, i)
end,
"rust named captures" => fn i ->
RegexRs.named_captures(rust_named_capture_regex, i)
end,
"elixir named captures" => fn i ->
Regex.named_captures(elixir_named_capture_regex, i)
end,
"rust run" => fn i ->
RegexRs.run(rust_run_regex, i)
end,
"elixir run" => fn i ->
Regex.run(elixir_run_regex, i)
end
},
inputs: %{
"small hit" => small_hit_str,
"small miss" => small_miss_str,
"big hit" => big_hit_str,
"big miss" => big_miss_str
}
)
clark@doomguy:~/code/personal/regex_example$ MIX_ENV=prod mix run bench.exs
==> regex_rs
Compiling NIF crate :regexrust (native/regexrust)...
Compiling regexrust v0.1.0 (/home/clark/code/personal/regex_rs/native/regexrust)
Finished release [optimized] target(s) in 1.15s
Operating System: Linux
CPU Information: AMD Ryzen 7 5800X 8-Core Processor
Number of Available Cores: 16
Available memory: 50.17 GB
Elixir 1.11.2
Erlang 23.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: big hit, big miss, small hit, small miss
Estimated total run time: 3.73 min
Benchmarking elixir match with input big hit...
Benchmarking elixir match with input big miss...
Benchmarking elixir match with input small hit...
Benchmarking elixir match with input small miss...
Benchmarking elixir named captures with input big hit...
Benchmarking elixir named captures with input big miss...
Benchmarking elixir named captures with input small hit...
Benchmarking elixir named captures with input small miss...
Benchmarking elixir run with input big hit...
Benchmarking elixir run with input big miss...
Benchmarking elixir run with input small hit...
Benchmarking elixir run with input small miss...
Benchmarking elixir scan with input big hit...
Benchmarking elixir scan with input big miss...
Benchmarking elixir scan with input small hit...
Benchmarking elixir scan with input small miss...
Benchmarking rust match with input big hit...
Benchmarking rust match with input big miss...
Benchmarking rust match with input small hit...
Benchmarking rust match with input small miss...
Benchmarking rust named captures with input big hit...
Benchmarking rust named captures with input big miss...
Benchmarking rust named captures with input small hit...
Benchmarking rust named captures with input small miss...
Benchmarking rust run with input big hit...
Benchmarking rust run with input big miss...
Benchmarking rust run with input small hit...
Benchmarking rust run with input small miss...
Benchmarking rust scan with input big hit...
Benchmarking rust scan with input big miss...
Benchmarking rust scan with input small hit...
Benchmarking rust scan with input small miss...
##### With input big hit #####
Name ips average deviation median 99th %
rust match 3324.95 K 0.30 μs ±58.55% 0.30 μs 0.40 μs
elixir match 1074.33 K 0.93 μs ±1852.06% 0.80 μs 1.80 μs
rust named captures 956.32 K 1.05 μs ±1646.73% 0.90 μs 1.40 μs
rust run 907.40 K 1.10 μs ±1020.93% 1 μs 1.40 μs
elixir run 894.92 K 1.12 μs ±1274.54% 1 μs 2 μs
elixir named captures 633.26 K 1.58 μs ±795.07% 1.50 μs 2.70 μs
rust scan 4.23 K 236.66 μs ±51.91% 178.40 μs 371.30 μs
elixir scan 0.54 K 1854.03 μs ±1.67% 1850 μs 1973.54 μs
Comparison:
rust match 3324.95 K
elixir match 1074.33 K - 3.09x slower +0.63 μs
rust named captures 956.32 K - 3.48x slower +0.74 μs
rust run 907.40 K - 3.66x slower +0.80 μs
elixir run 894.92 K - 3.72x slower +0.82 μs
elixir named captures 633.26 K - 5.25x slower +1.28 μs
rust scan 4.23 K - 786.88x slower +236.36 μs
elixir scan 0.54 K - 6164.56x slower +1853.73 μs
##### With input big miss #####
Name ips average deviation median 99th %
rust scan 52.18 K 19.17 μs ±6.09% 18.90 μs 25.10 μs
rust match 51.50 K 19.42 μs ±216.77% 19.10 μs 24 μs
rust run 51.00 K 19.61 μs ±5.45% 19.40 μs 24.50 μs
rust named captures 50.32 K 19.87 μs ±5.05% 19.50 μs 23.60 μs
elixir named captures 3.08 K 324.94 μs ±1.53% 324.20 μs 340.20 μs
elixir match 2.63 K 380.88 μs ±2.20% 379.10 μs 412.51 μs
elixir scan 2.62 K 381.49 μs ±1.70% 380.80 μs 405.22 μs
elixir run 2.20 K 454.90 μs ±1.69% 453.90 μs 478.43 μs
Comparison:
rust scan 52.18 K
rust match 51.50 K - 1.01x slower +0.25 μs
rust run 51.00 K - 1.02x slower +0.44 μs
rust named captures 50.32 K - 1.04x slower +0.71 μs
elixir named captures 3.08 K - 16.95x slower +305.77 μs
elixir match 2.63 K - 19.87x slower +361.71 μs
elixir scan 2.62 K - 19.90x slower +362.32 μs
elixir run 2.20 K - 23.74x slower +435.73 μs
##### With input small hit #####
Name ips average deviation median 99th %
rust match 12.48 M 0.0802 μs ±167.79% 0.100 μs 0.100 μs
rust scan 1.91 M 0.52 μs ±4030.63% 0.40 μs 0.80 μs
rust named captures 1.56 M 0.64 μs ±3916.79% 0.50 μs 1.30 μs
rust run 1.48 M 0.68 μs ±2924.90% 0.60 μs 0.90 μs
elixir match 0.99 M 1.01 μs ±1821.68% 0.90 μs 1.90 μs
elixir run 0.84 M 1.20 μs ±1390.26% 1.10 μs 2.10 μs
elixir named captures 0.59 M 1.68 μs ±841.44% 1.50 μs 2.70 μs
elixir scan 0.31 M 3.19 μs ±428.66% 2.90 μs 4.90 μs
Comparison:
rust match 12.48 M
rust scan 1.91 M - 6.54x slower +0.44 μs
rust named captures 1.56 M - 8.01x slower +0.56 μs
rust run 1.48 M - 8.45x slower +0.60 μs
elixir match 0.99 M - 12.62x slower +0.93 μs
elixir run 0.84 M - 14.93x slower +1.12 μs
elixir named captures 0.59 M - 20.98x slower +1.60 μs
elixir scan 0.31 M - 39.78x slower +3.11 μs
##### With input small miss #####
Name ips average deviation median 99th %
rust match 10.33 M 0.0968 μs ±5364.21% 0.100 μs 0.100 μs
rust scan 8.34 M 0.120 μs ±206.29% 0.100 μs 0.20 μs
rust run 7.23 M 0.138 μs ±170.41% 0.100 μs 0.20 μs
rust named captures 5.72 M 0.175 μs ±216.83% 0.20 μs 0.30 μs
elixir run 0.77 M 1.30 μs ±1194.69% 1.30 μs 1.40 μs
elixir match 0.74 M 1.35 μs ±980.90% 1.30 μs 2.20 μs
elixir scan 0.61 M 1.63 μs ±903.36% 1.50 μs 2.50 μs
elixir named captures 0.57 M 1.76 μs ±853.90% 1.60 μs 2.60 μs
Comparison:
rust match 10.33 M
rust scan 8.34 M - 1.24x slower +0.0231 μs
rust run 7.23 M - 1.43x slower +0.0415 μs
rust named captures 5.72 M - 1.81x slower +0.0780 μs
elixir run 0.77 M - 13.42x slower +1.20 μs
elixir match 0.74 M - 13.90x slower +1.25 μs
elixir scan 0.61 M - 16.79x slower +1.53 μs
elixir named captures 0.57 M - 18.20x slower +1.67 μs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment