Skip to content

Instantly share code, notes, and snippets.

@aifrak
Last active July 20, 2022 13:11
Show Gist options
  • Select an option

  • Save aifrak/a3f7b3edbbdf83efddb155b778f62bb4 to your computer and use it in GitHub Desktop.

Select an option

Save aifrak/a3f7b3edbbdf83efddb155b778f62bb4 to your computer and use it in GitHub Desktop.
Calculate average execution time inside IEX without third-part libraries
repeat = 10
microseconds = 1_000_000
max_concurrency = System.schedulers_online()
test_func = fn -> :timer.tc(PerfectNumbers, :aliquot_sum, [33_550_335]) |> elem(0) end
recompile && Stream.repeatedly(fn -> test_func end) |> Task.async_stream(fn fun -> fun.() end, timeout: :infinity, ordered: false, max_concurrency: max_concurrency) |> Stream.map(fn {:ok, r} -> r end) |> Enum.take(repeat) |> then(&(Enum.sum(&1) / repeat) / microseconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment