-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbinary-split.escript
executable file
·91 lines (78 loc) · 2.49 KB
/
binary-split.escript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env escript
%%%
%%% What's the most efficient way to split a binary into two parts using a
%%% pattern?
%%%
%%% This is the sister benchmark to binary-match.
%%%
%%% There are three approaches I can think of:
%%%
%%% - Use binary:match/2 to find the pattern position and split_binary
%%% to return the parts
%%% - Use binary:split/2
%%% - Use pattern matching
%%%
%%% Typical results on my laptop under R16B:
%%%
%%% binary_match: 67
%%% binary_split: 79
%%% binary_pattern: 3617
%%%
%%% Based on binary-match.escript results, nothing surprising here.
%%% binary:split/2 is the one to use (though strangely, with more moving parts,
%%% the binary:match/2 method is ever so slightly faster).
%%%
-mode(compile).
-include("bench.hrl").
-define(NEEDLE, <<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16>>).
-define(TRIALS, 100000).
main(_) ->
test_binary_match(),
test_binary_split(),
test_binary_pattern().
haystack() ->
NeedleRev = lists:reverse(binary_to_list(?NEEDLE)),
Parts =
[lists:duplicate(20, NeedleRev),
?NEEDLE,
lists:duplicate(20, NeedleRev)],
iolist_to_binary(Parts).
test_binary_match() ->
Haystack = haystack(),
Target = binary:split(Haystack, ?NEEDLE),
bench(
"binary_match",
fun() -> binary_match(?NEEDLE, Haystack, Target) end,
?TRIALS).
binary_match(Needle, Haystack, Target) ->
{Pos, Len} = binary:match(Haystack, Needle),
{P1, Rest} = split_binary(Haystack, Pos),
{_, P2} = split_binary(Rest, Len),
Target = [P1, P2].
test_binary_split() ->
Haystack = haystack(),
Target = binary:split(Haystack, ?NEEDLE),
bench(
"binary_split",
fun() -> binary_split(?NEEDLE, Haystack, Target) end,
?TRIALS).
binary_split(Needle, Haystack, Target) ->
Target = binary:split(Haystack, Needle).
test_binary_pattern() ->
Haystack = haystack(),
Target = binary:split(Haystack, ?NEEDLE),
bench(
"binary_pattern",
fun() -> binary_pattern(?NEEDLE, Haystack, Target) end,
?TRIALS).
binary_pattern(Needle, Haystack, Target) ->
NeedleSize = size(Needle),
Start = 0,
Stop = size(Haystack) - NeedleSize,
Target = binary_pattern(Needle, NeedleSize, Haystack, Start, Stop).
binary_pattern(Needle, NeedleSize, Haystack, Pos, Stop) when Pos =< Stop ->
case Haystack of
<<P1:Pos/binary, Needle:NeedleSize/binary, P2/binary>> -> [P1, P2];
_ -> binary_pattern(Needle, NeedleSize, Haystack, Pos + 1, Stop)
end;
binary_pattern(_, _, _, _, _) -> not_found.