-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdrop-last.escript
executable file
·69 lines (59 loc) · 1.57 KB
/
drop-last.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
#!/usr/bin/env escript
%%%
%%% This test is driven by a proposal to implement a drop_last function in
%%% Erlang's lists module.
%%%
%%% http://erlang.org/pipermail/erlang-patches/2013-April/003851.html
%%%
%%% Representative of my results (laptop):
%%%
%%% reverse_reverse:small_list: 166
%%% reverse_reverse:big_list: 3017
%%% recurse:small_list: 210
%%% recurse:small_list: 3115
%%% sublist:small_list: 316
%%% sublist:big_list: 9558
%%%
-mode(compile).
-include("bench.hrl").
-define(SMALL_LIST, lists:seq(1, 100)).
-define(SMALL_LIST_TRIALS, 100000).
-define(BIG_LIST, lists:seq(1, 1000000)).
-define(BIG_LIST_TRIALS, 100).
main(_) ->
test_reverse_reverse(),
test_recurse(),
test_sublist().
test_reverse_reverse() ->
bench(
"reverse_reverse:small_list",
fun() -> reverse_reverse(?SMALL_LIST) end,
?SMALL_LIST_TRIALS),
bench(
"reverse_reverse:big_list",
fun() -> reverse_reverse(?BIG_LIST) end,
?BIG_LIST_TRIALS).
reverse_reverse(L) ->
lists:reverse(tl(lists:reverse(L))).
test_recurse() ->
bench(
"recurse:small_list",
fun() -> recurse(?SMALL_LIST) end,
?SMALL_LIST_TRIALS),
bench(
"recurse:big_list",
fun() -> recurse(?BIG_LIST) end,
?BIG_LIST_TRIALS).
recurse([_]) -> [];
recurse([H|T]) -> [H|recurse(T)].
test_sublist() ->
bench(
"sublist:small_list",
fun() -> sublist(?SMALL_LIST) end,
?SMALL_LIST_TRIALS),
bench(
"sublist:big_list",
fun() -> sublist(?BIG_LIST) end,
?BIG_LIST_TRIALS).
sublist(L) ->
lists:sublist(L, 1, length(L) - 1).