Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Attempted benchmarking of erlang sub-string matching functions "string:str/2" and "re:run/2". ```re:run/2``` comes off worse in this shown down because of the initial time needed to compile the regex. It's sufficiently scorching after that but it still never manages to catch up to ```string:str/2```. Given that I intended to use one of these fun…
-module(test_str).
-compile(export_all).
strs() ->
[" foo |# 3 3)", " foo #| bar |# foobar |# 3)"].
run_timing(Mod, Fun, [NoMatch|Match]) ->
{MatchTime, _} = timer:tc(Mod, Fun, Match),
{NoMatchTime, _} = timer:tc(Mod, Fun, NoMatch),
{no_match, NoMatchTime, match, MatchTime}.
time_it([X|Y], str) ->
MatchStr = "#|",
run_timing(string, str, [[X, MatchStr],[Y, MatchStr]]);
time_it([X|Y], regex) ->
MatchStr = "(#\\|)+",
run_timing(re, run, [[X, MatchStr],[Y, MatchStr]]).
run_test(Type) -> [time_it(strs(), Type) || _ <- lists:seq(0,10000)].
get_avg(Type) ->
Times = run_test(Type),
TotalRuns = length(Times),
{NoMatchSum, MatchSum} = lists:foldl(
fun ({_, NoMatchRun, _, MatchRun}, {XSum, YSum}) ->
{XSum + NoMatchRun, YSum + MatchRun}
end,
{0, 0}, Times),
{no_match, NoMatchSum / TotalRuns, match, MatchSum / TotalRuns}.
Owner

mankyKitty commented Jan 22, 2014

Attempted benchmarking of erlang sub-string matching functions "string:str/2" and "re:run/2". re:run/2 comes off worse in this shown down because of the initial time needed to compile the regex. It's sufficiently scorching after that but it still never manages to catch up to string:str/2. Given that I intended to use one of these functions in a situation that may not allow the compiled regex to be maintained for every time the function is called, I think I'll just use string:str/2. It's also a little simpler because I don't have to use regex. They're awesome, but we're not really friends.

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