Skip to content

Instantly share code, notes, and snippets.

@RumataEstor
Created October 3, 2013 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RumataEstor/6806153 to your computer and use it in GitHub Desktop.
Save RumataEstor/6806153 to your computer and use it in GitHub Desktop.
-module(binaries).
-export([by_char/2, whole/2, by_list/2, count/0]).
by_char(Rest, <<>>) ->
Rest;
by_char(<<C, String/binary>>, <<C, Pattern/binary>>) ->
by_char(String, Pattern);
by_char(_, _) ->
no_match.
whole(String, Pattern) ->
PatternSize = byte_size(Pattern),
case String of
<<Pattern:PatternSize/binary, Rest/binary>> ->
Rest;
_ ->
no_match
end.
by_list(String, Boundary) ->
by_list0(String, binary_to_list(Boundary)).
by_list0(<<C, String/binary>>, [C | Pattern]) ->
by_list0(String, Pattern);
by_list0(Rest, []) ->
Rest;
by_list0(_, _) ->
no_match.
count() ->
[count(Fun, 1000, 0) || Fun <- [by_char, by_list, whole]].
count(Fun, 0, Result) ->
{Fun, Result};
count(Fun, N, Result) ->
S = crypto:rand_bytes(2000),
B = binary:part(S, {0, 20}),
{Time, _} = timer:tc(?MODULE, Fun, [S, B]),
count(Fun, N - 1, Result + Time).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment