-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcomma-parse.escript
executable file
·93 lines (81 loc) · 2.49 KB
/
comma-parse.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
92
93
#!/usr/bin/env escript
%%%
%%% This test is driven by erlydtl's inability to support yesno filter with
%%% empty values. E.g. "var|yesno:'if-true,'" will fail. It shouldn't.
%%%
%%% The yesno filter uses string:token/2 to parse the string. I was curious
%%% if switching to re:split/3 would cause a significant performance hit.
%%%
%%% Typical results on my laptop under R16B:
%%%
%%% string_tokens: 291
%%% uncompiled_re_split: 2515
%%% compiled_re_split: 2051
%%% binary_split: 311
%%% binary_split_with_convert: 457
%%%
-mode(compile).
-include("bench.hrl").
-define(TRIALS, 100000).
-define(STRINGS,
["yes,no,default",
"yes,",
"yes,,",
"01234567890123456789,01234567890123456789,01234567890123456789"]).
-define(COMMA, ",").
-define(COMMA_REGEX,
{re_pattern,0,0,
<<69,82,67,80,57,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,44,0,0,0,48,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,0,5,27,44,
84,0,5,0>>}).
main(_) ->
test_string_tokens(),
test_uncompiled_re_split(),
test_compiled_re_split(),
test_bin_split(),
test_convert_bin_split().
test_string_tokens() ->
bench(
"string_tokens",
fun() -> string_tokenize(?STRINGS, ?COMMA) end,
?TRIALS).
string_tokenize([S|Rest], Delimiter) ->
string:tokens(S, Delimiter),
string_tokenize(Rest, Delimiter);
string_tokenize([], _Delim) -> ok.
test_uncompiled_re_split() ->
bench(
"uncompiled_re_split",
fun() -> re_split(?STRINGS, ?COMMA) end,
?TRIALS).
re_split([S|Rest], Pattern) ->
re:split(S, Pattern, [{return, list}]),
re_split(Rest, Pattern);
re_split([], _Pattern) -> ok.
test_compiled_re_split() ->
bench(
"compiled_re_split",
fun() -> re_split(?STRINGS, ?COMMA_REGEX) end,
?TRIALS).
test_bin_split() ->
BinStrings = [list_to_binary(S) || S <- ?STRINGS],
BinComma = list_to_binary(?COMMA),
bench(
"binary_split",
fun() -> bin_split(BinStrings, BinComma) end,
?TRIALS).
bin_split([S|Rest], Sep) ->
binary:split(S, Sep, [global]),
bin_split(Rest, Sep);
bin_split([], _Sep) -> ok.
test_convert_bin_split() ->
bench(
"binary_split_with_convert",
fun() -> convert_bin_split(?STRINGS, ?COMMA) end,
?TRIALS).
convert_bin_split([S|Rest], Sep) ->
SBin = list_to_binary(S),
SepBin = list_to_binary(Sep),
_ = [binary_to_list(Part) || Part <- binary:split(SBin, SepBin, [global])],
convert_bin_split(Rest, Sep);
convert_bin_split([], _Sep) -> ok.