Last active
January 4, 2016 02:59
-
-
Save mankyKitty/8558705 to your computer and use it in GitHub Desktop.
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 tostring: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 usestring: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.