Last active
December 17, 2022 13:42
-
-
Save reisub/9b89791f726a7270405efe5b3fa77c3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output with an M1 Max MBP, macOS 13.0.1, Elixir 1.14.1, Erlang 25.1.1: