Skip to content

Instantly share code, notes, and snippets.

@reisub
Last active December 17, 2022 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reisub/9b89791f726a7270405efe5b3fa77c3e to your computer and use it in GitHub Desktop.
Save reisub/9b89791f726a7270405efe5b3fa77c3e to your computer and use it in GitHub Desktop.
Mix.install([:table_rex])
defmodule Benchmark do
def run(iterations, fun) do
durations = Enum.map(1..iterations, fn _ -> measure_time(fun) end)
%{
"average (us)" => Enum.sum(durations) / iterations,
"median (us)" => Enum.sort(durations) |> Enum.at(round((iterations - 1) / 2)),
"min (us)" => Enum.min(durations),
"max (us)" => Enum.max(durations)
}
end
def show(measurements) do
{header, data} = Enum.unzip(measurements)
TableRex.quick_render!([data], header)
|> IO.puts()
end
defp measure_time(fun) do
{duration, _} = :timer.tc(fun)
duration
end
end
System.version()
old = "some-short-string"
new = "a-bit-longer-but-short-still"
Benchmark.run(500_000, fn -> String.myers_difference(old, new) end) |> Benchmark.show()
Benchmark.run(500_000, fn -> Task.async(fn -> String.myers_difference(old, new) end) |> Task.await() end) |> Benchmark.show()
@reisub
Copy link
Author

reisub commented Dec 17, 2022

Output with an M1 Max MBP, macOS 13.0.1, Elixir 1.14.1, Erlang 25.1.1:

+--------------+----------+-------------+----------+
| average (us) | max (us) | median (us) | min (us) |
+--------------+----------+-------------+----------+
| 10.38885     | 3709     | 10          | 9        |
+--------------+----------+-------------+----------+

+--------------+----------+-------------+----------+
| average (us) | max (us) | median (us) | min (us) |
+--------------+----------+-------------+----------+
| 25.33128     | 2931     | 25          | 24       |
+--------------+----------+-------------+----------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment