Timeit

This module provides a helper function for very simply measuring code execution time in IEx, Elixir's command line console.

iex> import Timeit

iex> timeit 1 + 2
5μs
3

You can optionally provide a label and/or change the time unit.

iex> timeit :nanosecond, 1 + 2
5000μs
3

iex> timeit "addition", 1 + 2
addition: 5μs
3

iex> timeit :nanosecond, 1 + 2
addition: 5000μs
3

The result of the expression is returned by timeit. This is helpful for profiling different parts of a complex expression separately.

iex> x = timeit("first part", 3 * 3) + timeit("second part", 4 * 4)
first part: 13μs
second part: 6μs
25

You can also do assignment within the expression and it works, because macros.

iex> timeit x = 1 + 2
4μs
3
iex> x
3

Installation

Add timeit to your list of dependencies in mix.exs:

def deps do
  [
    {:timeit, "~> 0.1.0"}
  ]
end

Also I recommend importing it in your .iex.exs file like so:

import Timeit