-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstrip-binary.escript
executable file
·58 lines (50 loc) · 1.54 KB
/
strip-binary.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
#!/usr/bin/env escript
%%%
%%% It's pretty common to trim spaces (or other characters) from a
%%% string. But what if the string is represented by a binary? Does
%%% it make sense to convert the binary to a list and apply
%%% string:strip to it? Or is there a better way?
%%%
%%% The alternative explored here is taken from this informative
%%% thread:
%%%
%%% http://erlang.org/pipermail/erlang-questions/2009-June/044786.html
%%%
%%% Representative of my results (laptop):
%%%
%%% string_strip: 0.724 us (1381816.67 per second)
%%% re_strip: 5.204 us (192149.17 per second)
%%% compiled_re_strip: 4.244 us (235607.28 per second)
%%%
%%% So stupid is apparently a lot faster!
%%%
-mode(compile).
-include("bench.hrl").
-define(STRING, <<" some_value_we_want_stripped ">>).
-define(STRIPPED, <<"some_value_we_want_stripped">>).
-define(STRIP_RE, "^\\s+|\\s+$").
-define(TRIALS, 1000000).
main(_) ->
test_string_strip(),
test_re_strip(),
test_compiled_re_strip().
test_string_strip() ->
bench(
"string_strip",
fun() -> string_strip(?STRING) end,
?TRIALS).
string_strip(Str) ->
?STRIPPED = list_to_binary(string:strip(binary_to_list(Str))).
test_re_strip() ->
bench(
"re_strip",
fun() -> re_strip(?STRING, ?STRIP_RE) end,
?TRIALS).
re_strip(Str, RE) ->
?STRIPPED = re:replace(Str, RE, "", [global, {return, binary}]).
test_compiled_re_strip() ->
{ok, Compiled} = re:compile(?STRIP_RE),
bench(
"compiled_re_strip",
fun() -> re_strip(?STRING, Compiled) end,
?TRIALS).