Skip to content

Instantly share code, notes, and snippets.

@tokubass
Last active October 11, 2017 05:49
Show Gist options
  • Save tokubass/7d7eacdf06ca6cd919f696ee7c210544 to your computer and use it in GitHub Desktop.
Save tokubass/7d7eacdf06ca6cd919f696ee7c210544 to your computer and use it in GitHub Desktop.
elixirの正規表現ベンチマーク
# TASKS=1000 elixir re_bench.exs pure_re.txt trie_re.txt
# elixirの正規表現実装がtrieなら速度に違いはないはず
# pureはhoge|fooと単純にパイプで繋いだ正規表現
# trieはPerlのRegexp::Assembleで生成した正規表現
[pure,trie|_] = System.argv
pure_regexp = File.read!(pure) |> Regex.compile!
trie_regexp = File.read!(trie) |> Regex.compile!
tasks = String.to_integer System.get_env("TASKS") || "1000"
# マッチさせる文字列を生成(あまり長いとマッチする正規表現がないのでa..gに)
list = for _list_size <- 1..20 do
?a..?g |> Enum.to_list |> Enum.shuffle |> List.to_string
end
:timer.tc(fn ->
IO.puts('== pure ==')
for _ <- 1..tasks do
for str <- list do
Regex.match?(pure_regexp, str)
end
end
end) |> elem(0) |> IO.puts
:timer.tc(fn ->
IO.puts('== trie ==')
for _ <- 1..tasks do
for str <- list do
Regex.match?(trie_regexp,str)
end
end
end) |> elem(0) |> IO.puts
use strict;
use Regexp::Assemble;
use List::Util qw/shuffle/;
my @word;
push @word, join '', shuffle ('a'..'g') for 0..500;
my $ra = Regexp::Assemble->new;
for (@word) {
$ra->add($_);
}
open(my $trie_fh, ">",'/home/ec2-user/trie_re.txt') or die $!;
print $trie_fh $ra->as_string;
open(my $fh, ">",'/home/ec2-user/pure_re.txt') or die $!;
print $fh (join '|' , @word);
@tokubass
Copy link
Author

trieのほうが速い

$ elixir re.exs pure_re.txt trie_re.txt
== pure ==
1232798
== trie ==
74532

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment