Skip to content

Instantly share code, notes, and snippets.

@dominicletz
Created June 26, 2024 08:35
Show Gist options
  • Save dominicletz/4a467f252e11356566dd289d9f804ae6 to your computer and use it in GitHub Desktop.
Save dominicletz/4a467f252e11356566dd289d9f804ae6 to your computer and use it in GitHub Desktop.
Test JIT optimizer
>elixir test.exs
Operating System: Linux
CPU Information: AMD Ryzen 9 6900HS with Radeon Graphics
Number of Available Cores: 16
Available memory: 30.58 GB
Elixir 1.17.0
Erlang 26.2.3
JIT enabled: true
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 18 s
Benchmarking dot_product_tco ...
Benchmarking dot_product_tco_guard ...
Calculating statistics...
Formatting results...
Name ips average deviation median 99th %
dot_product_tco_guard 79.34 K 12.60 μs ±87.28% 12.19 μs 19.47 μs
dot_product_tco 38.66 K 25.87 μs ±59.17% 24.64 μs 51.80 μs
Comparison:
dot_product_tco_guard 79.34 K
dot_product_tco 38.66 K - 2.05x slower +13.26 μs
Memory usage statistics:
Name Memory usage
dot_product_tco_guard 24 KB
dot_product_tco 0 KB - 0.00x memory usage -24 KB
**All measurements for memory usage were the same**
#!/usr/bin/env elixir
Mix.install([{:benchee, "~> 1.0"}])
defmodule Test do
def benchmark() do
vector1 = Enum.map(1..1536, fn _ -> :rand.uniform() end)
vector2 = Enum.map(1..1536, fn _ -> :rand.uniform() end)
Benchee.run(
%{
"dot_product_tco" => fn -> dot_product_tco(vector1, vector2) end,
"dot_product_tco_guard" => fn -> dot_product_tco_guard(vector1, vector2) end
},
time: 5,
memory_time: 2
)
end
def dot_product_tco(vec1, vec2, acc \\ 0)
def dot_product_tco([], [], acc), do: acc
def dot_product_tco([x | vector1], [y | vector2], acc) do
dot_product_tco(vector1, vector2, x * y + acc)
end
def dot_product_tco_guard(vec1, vec2, acc \\ 0)
def dot_product_tco_guard([], [], acc), do: acc
def dot_product_tco_guard([x | vector1], [y | vector2], acc) when is_float(x) and is_float(y) do
dot_product_tco_guard(vector1, vector2, x * y + acc)
end
end
Test.benchmark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment