Skip to content

Instantly share code, notes, and snippets.

@madlep
Created December 21, 2022 18:05
Show Gist options
  • Save madlep/6a4a15878edc04af435d45c8d54a336b to your computer and use it in GitHub Desktop.
Save madlep/6a4a15878edc04af435d45c8d54a336b to your computer and use it in GitHub Desktop.
fib benchmarks
defmodule Fib do
defmodule MLibby do
def number(n) do
[head | _] = _sequence(n)
head
end
def sequence(n), do: Enum.reverse(_sequence(n))
defp _sequence(0), do: [0]
defp _sequence(1), do: [1, 0]
defp _sequence(n) do
seq = _sequence(n - 1)
[a, b | _] = seq
[a + b | seq]
end
end
defmodule Madlep do
def number(n), do: num({0, 1}, n)
defp num({current, next}, n) when n > 0, do: num({next, current + next}, n - 1)
defp num({current, _next}, 0), do: current
def sequence(n), do: seq({0, 1}, n, [])
defp seq({current, next}, n, acc) when n > 0,
do: seq({next, current + next}, n - 1, [current | acc])
defp seq({current, _next}, 0, acc), do: Enum.reverse([current | acc])
end
end
Benchee.run(
%{
"mlibby number" => &Fib.MLibby.number/1,
"madlep number" => &Fib.Madlep.number/1,
},
inputs: %{
"100" => 100,
"1000" => 1000,
"10000" => 10000,
"100000" => 100000,
},
time: 5,
memory_time: 5,
reduction_time: 5
)
Compiling 1 file (.ex)
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.14.1
Erlang 25.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 5 s
reduction time: 5 s
parallel: 1
inputs: 100, 1000, 10000, 100000
Estimated total run time: 2.27 min
Benchmarking madlep number with input 100 ...
Benchmarking madlep number with input 1000 ...
Benchmarking madlep number with input 10000 ...
Benchmarking madlep number with input 100000 ...
Benchmarking mlibby number with input 100 ...
Benchmarking mlibby number with input 1000 ...
Benchmarking mlibby number with input 10000 ...
Benchmarking mlibby number with input 100000 ...
##### With input 100 #####
Name ips average deviation median 99th %
madlep number 1.73 M 0.58 μs ±61.10% 0.54 μs 1.17 μs
mlibby number 0.78 M 1.28 μs ±280.32% 1.21 μs 2.42 μs
Comparison:
madlep number 1.73 M
mlibby number 0.78 M - 2.22x slower +0.70 μs
Memory usage statistics:
Name Memory usage
madlep number 2.34 KB
mlibby number 1.55 KB - 0.66x memory usage -0.79688 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
madlep number 103
mlibby number 201 - 1.95x reduction count +98
**All measurements for reduction count were the same**
##### With input 1000 #####
Name ips average deviation median 99th %
madlep number 53.75 K 18.60 μs ±30.01% 17.08 μs 42.75 μs
mlibby number 35.12 K 28.48 μs ±20.46% 25.67 μs 49.50 μs
Comparison:
madlep number 53.75 K
mlibby number 35.12 K - 1.53x slower +9.87 μs
Memory usage statistics:
Name Memory usage
madlep number 23.44 KB
mlibby number 15.61 KB - 0.67x memory usage -7.82813 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
madlep number 1.01 K
mlibby number 2.38 K - 2.37x reduction count +1.38 K
**All measurements for reduction count were the same**
##### With input 10000 #####
Name ips average deviation median 99th %
madlep number 818.88 1.22 ms ±14.28% 1.19 ms 1.65 ms
mlibby number 724.23 1.38 ms ±19.99% 1.38 ms 2.01 ms
Comparison:
madlep number 818.88
mlibby number 724.23 - 1.13x slower +0.160 ms
Memory usage statistics:
Name Memory usage
madlep number 234.38 KB
mlibby number 156.23 KB - 0.67x memory usage -78.14063 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
madlep number 10.71 K ±0.00% 10.71 K 10.71 K
mlibby number 26.62 K ±0.20% 26.61 K 26.78 K
Comparison:
madlep number 10.71 K
mlibby number 26.62 K - 2.49x reduction count +15.91 K
##### With input 100000 #####
Name ips average deviation median 99th %
madlep number 7.78 128.50 ms ±7.20% 128.54 ms 152.42 ms
mlibby number 4.18 239.40 ms ±13.23% 224.34 ms 340.96 ms
Comparison:
madlep number 7.78
mlibby number 4.18 - 1.86x slower +110.90 ms
Memory usage statistics:
Name Memory usage
madlep number 2.29 MB
mlibby number 1.53 MB - 0.67x memory usage -0.76240 MB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
madlep number 0.149 M ±7.46% 0.146 M 0.179 M
mlibby number 1.01 M ±2.55% 1.01 M 1.06 M
Comparison:
madlep number 0.146 M
mlibby number 1.01 M - 6.78x reduction count +0.86 M
Benchee.run(
%{
"mlibby sequence" => &Fib.MLibby.sequence/1,
"madlep sequence" => &Fib.Madlep.sequence/1
},
inputs: %{
"100" => 100,
"1000" => 1000,
"10000" => 10000,
"100000" => 100000,
},
time: 5,
memory_time: 5,
reduction_time: 5
)
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.14.1
Erlang 25.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 5 s
reduction time: 5 s
parallel: 1
inputs: 100, 1000, 10000, 100000
Estimated total run time: 2.27 min
Benchmarking madlep sequence with input 100 ...
Benchmarking madlep sequence with input 1000 ...
Benchmarking madlep sequence with input 10000 ...
Benchmarking madlep sequence with input 100000 ...
Benchmarking mlibby sequence with input 100 ...
Benchmarking mlibby sequence with input 1000 ...
Benchmarking mlibby sequence with input 10000 ...
Benchmarking mlibby sequence with input 100000 ...
##### With input 100 #####
Name ips average deviation median 99th %
madlep sequence 1.10 M 0.91 μs ±2008.62% 0.71 μs 2.75 μs
mlibby sequence 0.65 M 1.54 μs ±1089.05% 1.38 μs 2.92 μs
Comparison:
madlep sequence 1.10 M
mlibby sequence 0.65 M - 1.69x slower +0.63 μs
Memory usage statistics:
Name Memory usage
madlep sequence 5.50 KB
mlibby sequence 1.58 KB - 0.29x memory usage -3.92188 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
madlep sequence 150
mlibby sequence 228 - 1.52x reduction count +78
**All measurements for reduction count were the same**
##### With input 1000 #####
Name ips average deviation median 99th %
madlep sequence 45.38 K 22.04 μs ±26.85% 18.58 μs 39.69 μs
mlibby sequence 32.84 K 30.45 μs ±19.98% 26.96 μs 50.75 μs
Comparison:
madlep sequence 45.38 K
mlibby sequence 32.84 K - 1.38x slower +8.42 μs
Memory usage statistics:
Name Memory usage
madlep sequence 54.72 KB
mlibby sequence 29.95 KB - 0.55x memory usage -24.76563 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
madlep sequence 2.63 K
mlibby sequence 3.28 K - 1.24x reduction count +0.64 K
**All measurements for reduction count were the same**
##### With input 10000 #####
Name ips average deviation median 99th %
madlep sequence 730.13 1.37 ms ±32.63% 1.33 ms 2.40 ms
mlibby sequence 695.04 1.44 ms ±34.26% 1.17 ms 2.85 ms
Comparison:
madlep sequence 730.13
mlibby sequence 695.04 - 1.05x slower +0.0691 ms
Memory usage statistics:
Name Memory usage
madlep sequence 546.91 KB
mlibby sequence 312.50 KB - 0.57x memory usage -234.40625 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
madlep sequence 14.79 K ±0.50% 14.78 K 14.99 K
mlibby sequence 28.25 K ±1.02% 28.12 K 29.19 K
Comparison:
madlep sequence 14.78 K
mlibby sequence 28.25 K - 1.91x reduction count +13.46 K
##### With input 100000 #####
Name ips average deviation median 99th %
mlibby sequence 3.80 263.05 ms ±15.54% 256.00 ms 377.26 ms
madlep sequence 3.65 274.10 ms ±33.00% 267.38 ms 553.88 ms
Comparison:
mlibby sequence 3.80
madlep sequence 3.65 - 1.04x slower +11.05 ms
Memory usage statistics:
Name Memory usage
mlibby sequence 3.05 MB
madlep sequence 5.34 MB - 1.75x memory usage +2.29 MB
**All measurements for memory usage were the same**
Reduction count statistics:
Name average deviation median 99th %
mlibby sequence 1.03 M ±4.23% 1.01 M 1.12 M
madlep sequence 0.69 M ±5.56% 0.68 M 0.78 M
Comparison:
mlibby sequence 1.01 M
madlep sequence 0.69 M - 0.67x reduction count -0.34119 M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment