elixirian · elixir · recipes · iex · benchmark

Elixiran | Recipes | Recipe 4: Benchmark in IEx

🍳 Recipe 4: Simple Benchmarking in IEx without Benchee

# file: slow_math.ex

defmodule SlowMath do
  @moduledoc """
  Demonstrates a slow operation for benchmarking.
  """

  @doc """
  Artificially slow sum using sleep in reduce.
  """
  def slow_sum(n) do
    Enum.reduce(1..n, 0, &slow_add/2)
  end

  @doc """
  Simulates a slow addition operation.
  """
  def slow_add(x, acc) do
    # Simulate delay
    Process.sleep(1)
    acc + x
  end
end

In IEx:

Code.require_file("./slow_math.ex")
{time_us, result} = :timer.tc(fn -> SlowMath.slow_sum(10) end)
IO.puts("Time: #{time_us / 1_000} ms")

recipes 4, 1: simple benchmark in IEx