Is there an equivalent python time module in Elixir?
In python, you can spend small chunks of code execution time using the timeit module.
https://docs.python.org/2/library/timeit.html
Is there an equivalent in Elixir?
source to share
The simplest tool would be to use the Erlang module :timer
with one of its variants tc
. It returns the execution time in microseconds and the result of the function as a tuple. By the time of the anonymous function, you can do
{time, res} = :timer.tc fn -> :timer.sleep(1000) end
# {1000575, :ok}
{time, res} = :timer.tc(fn(ms) -> :timer.sleep(ms) end, [1000])
# {1001283, :ok}
To execute a module function, you can do
defmodule Sleepy do
def sleep(ms) do
:timer.sleep(ms)
end
end
{time, res} = :timer.tc(&Sleepy.sleep/1, [1000])
# {1001106, :ok}
{time, res} = :timer.tc(Sleepy, :sleep, [1000])
# {1000352, :ok}
Timex is another option that has a more user-friendly API for measuring time, among other things, however if you only need something time is :timer
more than enough with a little wrapper code.
source to share