Timing A Function In Elixir
Erlang provides the :timer
module for all things timing. The oddly named function tc
will let you know how long a function takes:
{uSecs, :ok} = :timer.tc(IO, :puts, ["Hello World"])
Note that uSecs
is microseconds not milliseconds so divide by 1_000_000
to get seconds.
microseconds are helpful though because sometimes functions are just that quick.
:timer.tc(IO, :puts, ["Hello World"])
# {22, :ok}
You can also call :timer.tc
with a function and args:
adding = fn (x, y) -> x + y end
:timer.tc(adding, [1,3])
# {5, 4}
Or just a function:
:timer.tc(fn ->
# something really expensive
:ok
end)
# {1_302_342, :ok}
Tweet