Skip to content

Commit

Permalink
Benchmark module and top level differences
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara committed Dec 12, 2023
1 parent 528ac22 commit 5b1d99d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bench/compiler_optimizations.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
list = Enum.to_list(1..100)

defmodule Compiled do
def comprehension(list) do
for x <- list, rem(x, 2) == 1, do: x + 1
end
end

# Top level definitions used to be slower than module definitions.
# These were due to disabled compiler optimizations (no_bool_opt, no_ssa_opt)
# and have been fixed from elixir 1.16.0-rc.1:
# https://github.com/elixir-lang/elixir/commit/aabe46536e021da88de39bc5c82c90d97b0604ec#diff-1f4ed234972cb699fe6dc400f3bbf57f72919ef587ba6b1a43441c8b784b0cfb

Benchee.run(%{
"module (optimized)" => fn -> Compiled.comprehension(list) end,
"top_level (non-optimized)" => fn -> for x <- list, rem(x, 2) == 1, do: x + 1 end
})
26 changes: 26 additions & 0 deletions bench/compiler_optimizations.results.old.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Operating System: macOS
CPU Information: Apple M1
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.15.6
Erlang 26.1.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s

Benchmarking module (optimized) ...
Benchmarking top_level (non-optimized) ...

Name ips average deviation median 99th %
module (optimized) 1.74 M 576.11 ns ±4796.26% 500 ns 667 ns
top_level (non-optimized) 1.16 M 863.56 ns ±1966.89% 792 ns 958 ns

Comparison:
module (optimized) 1.74 M
top_level (non-optimized) 1.16 M - 1.50x slower +287.45 ns
26 changes: 26 additions & 0 deletions bench/compiler_optimizations.results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Operating System: macOS
CPU Information: Apple M1
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.16.0-rc.1
Erlang 26.1.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s

Benchmarking module (optimized) ...
Benchmarking top_level (non-optimized) ...

Name ips average deviation median 99th %
top_level (non-optimized) 1.77 M 565.93 ns ±3945.40% 500 ns 667 ns
module (optimized) 1.72 M 580.88 ns ±4537.27% 500 ns 667 ns

Comparison:
top_level (non-optimized) 1.77 M
module (optimized) 1.72 M - 1.03x slower +14.95 ns

0 comments on commit 5b1d99d

Please sign in to comment.