Skip to content

Commit 9b721f3

Browse files
committed
Benchmark local vs module functions
1 parent 62d60ea commit 9b721f3

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

bench/local_vs_module.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule Foo do
2+
def foo(x), do: 1 + x
3+
end
4+
5+
defmodule Bench do
6+
# uncomment to get S file
7+
# @compile :S
8+
9+
def module(enum) do
10+
Enum.each(enum, &Foo.foo/1)
11+
end
12+
13+
def local(enum) do
14+
Enum.each(enum, &foo/1)
15+
end
16+
17+
def foo(x), do: 1 + x
18+
end
19+
20+
inputs = [
21+
{"list", Enum.to_list(1..1000)}
22+
]
23+
24+
Benchee.run(
25+
[
26+
module: &Bench.module/1,
27+
local: &Bench.local/1
28+
],
29+
inputs: inputs
30+
)

bench/local_vs_module.exs.S

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{module, 'Elixir.Bench'}. %% version = 0
2+
3+
{exports, [{'__info__',1},
4+
{foo,1},
5+
{local,1},
6+
{module,1},
7+
{module_info,0},
8+
{module_info,1}]}.
9+
10+
{attributes, []}.
11+
12+
{labels, 22}.
13+
14+
15+
{function, '__info__', 1, 2}.
16+
{label,1}.
17+
{line,[]}.
18+
{func_info,{atom,'Elixir.Bench'},{atom,'__info__'},1}.
19+
{label,2}.
20+
{select_val,{x,0},
21+
{f,9},
22+
{list,[{atom,attributes},
23+
{f,8},
24+
{atom,compile},
25+
{f,8},
26+
{atom,deprecated},
27+
{f,7},
28+
{atom,exports_md5},
29+
{f,6},
30+
{atom,functions},
31+
{f,5},
32+
{atom,macros},
33+
{f,7},
34+
{atom,md5},
35+
{f,8},
36+
{atom,module},
37+
{f,4},
38+
{atom,struct},
39+
{f,3}]}}.
40+
{label,3}.
41+
{move,{atom,nil},{x,0}}.
42+
return.
43+
{label,4}.
44+
{move,{atom,'Elixir.Bench'},{x,0}}.
45+
return.
46+
{label,5}.
47+
{move,{literal,[{foo,1},{local,1},{module,1}]},{x,0}}.
48+
return.
49+
{label,6}.
50+
{move,{literal,<<249,220,148,233,109,149,24,191,155,116,221,51,167,22,151,
51+
183>>},
52+
{x,0}}.
53+
return.
54+
{label,7}.
55+
{move,nil,{x,0}}.
56+
return.
57+
{label,8}.
58+
{move,{x,0},{x,1}}.
59+
{move,{atom,'Elixir.Bench'},{x,0}}.
60+
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
61+
{label,9}.
62+
{call_only,1,{f,21}}. % '-inlined-__info__/1-'/1
63+
64+
65+
{function, foo, 1, 11}.
66+
{label,10}.
67+
{line,[{location,"bench/local_vs_module.exs",15}]}.
68+
{func_info,{atom,'Elixir.Bench'},{atom,foo},1}.
69+
{label,11}.
70+
{gc_bif,'+',{f,0},1,[{x,0},{integer,1}],{x,0}}.
71+
return.
72+
73+
74+
{function, local, 1, 13}.
75+
{label,12}.
76+
{line,[{location,"bench/local_vs_module.exs",11}]}.
77+
{func_info,{atom,'Elixir.Bench'},{atom,local},1}.
78+
{label,13}.
79+
{test_heap,{alloc,[{words,0},{floats,0},{funs,1}]},1}.
80+
{make_fun3,{f,11},0,0,{x,1},{list,[]}}.
81+
{'%',{var_info,{x,1},[{fun_type,{t_number,any}}]}}.
82+
{line,[{location,"bench/local_vs_module.exs",12}]}.
83+
{call_ext_only,2,{extfunc,'Elixir.Enum',each,2}}.
84+
85+
86+
{function, module, 1, 15}.
87+
{label,14}.
88+
{line,[{location,"bench/local_vs_module.exs",7}]}.
89+
{func_info,{atom,'Elixir.Bench'},{atom,module},1}.
90+
{label,15}.
91+
{move,{literal,fun 'Elixir.Foo':foo/1},{x,1}}.
92+
{line,[{location,"bench/local_vs_module.exs",8}]}.
93+
{call_ext_only,2,{extfunc,'Elixir.Enum',each,2}}.
94+
95+
96+
{function, module_info, 0, 17}.
97+
{label,16}.
98+
{line,[]}.
99+
{func_info,{atom,'Elixir.Bench'},{atom,module_info},0}.
100+
{label,17}.
101+
{move,{atom,'Elixir.Bench'},{x,0}}.
102+
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
103+
104+
105+
{function, module_info, 1, 19}.
106+
{label,18}.
107+
{line,[]}.
108+
{func_info,{atom,'Elixir.Bench'},{atom,module_info},1}.
109+
{label,19}.
110+
{move,{x,0},{x,1}}.
111+
{move,{atom,'Elixir.Bench'},{x,0}}.
112+
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
113+
114+
115+
{function, '-inlined-__info__/1-', 1, 21}.
116+
{label,20}.
117+
{line,[]}.
118+
{func_info,{atom,'Elixir.Bench'},{atom,'-inlined-__info__/1-'},1}.
119+
{label,21}.
120+
{jump,{f,20}}.

bench/local_vs_module.results.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Operating System: macOS
2+
CPU Information: Apple M1
3+
Number of Available Cores: 8
4+
Available memory: 16 GB
5+
Elixir 1.16.2
6+
Erlang 27.0
7+
8+
Benchmark suite executing with the following configuration:
9+
warmup: 2 s
10+
time: 5 s
11+
memory time: 0 ns
12+
reduction time: 0 ns
13+
parallel: 1
14+
inputs: list
15+
Estimated total run time: 14 s
16+
17+
Benchmarking module with input list ...
18+
Benchmarking local with input list ...
19+
20+
##### With input list #####
21+
Name ips average deviation median 99th %
22+
module 306.43 K 3.26 μs ±182.87% 3.21 μs 4.17 μs
23+
local 301.59 K 3.32 μs ±199.86% 3.21 μs 3.92 μs
24+
25+
Comparison:
26+
module 306.43 K
27+
local 301.59 K - 1.02x slower +0.0525 μs

0 commit comments

Comments
 (0)