GithubHelp home page GithubHelp logo

tim's Introduction

Hex.pm Version

Tim the tiny timer.

Sometimes you want a simple tool to estimate execution time. Tim can help!

Explanation and usage

Tim provides the macro time that takes any valid Elixir expression and returns a map containing several statistics for the expression's execution time, the result of the evaluated expression, and the expression's string representation. To use time, require or import Tim into your environment and pipe in the expression:

iex> require Tim
iex> 1..10 
...> |> Enum.map(& &1 * 2/((1 + 10) * 10)) 
...> |> Enum.sum()
...> |> Tim.time()
%{
  expr: "1..10 |> Enum.map(&(&1 * 2 / ((1 + 10) * 10))) |> Enum.sum()",
  max: 46,
  mean: 46.0,
  median: 46,
  min: 46,
  n: 1,
  result: 1.0,
  unit: :microsecond
}

Actual timing statistics will vary. By default, all times are in microseconds.

The time macro has two optional keyword arguments:

  • :n - number of times the expression is executed to gather timing statistics (defaults to 1)
  • :unit - unit of time in the reported statistics, which can be one of :microsecond (default), :millisecond, :second, :minute, or :hour

Tim also provides an inspect macro that applies IO.inspect to the timing data returned by Tim.time, but then returns the result of the expression being timed. This allows timing data to be captured in the middle of a pipeline. Like Tim.time, inspect also takes the optional keyword arguments :n, :unit, and :label. Here's an example of using inspect:

iex> require Tim
iex> :timer.sleep(1_000) |> Tim.inspect(n: 2, unit: :second, label: "Timing data")
Timing data: %{
  max: 1.000952,
  min: 1.00057,
  unit: :second,
  expr: ":timer.sleep(1000)",
  n: 2,
  mean: 1.000761,
  median: 1.000952
}
:ok

Under the hood

The body of the Tim.time macro wraps around Erlang's :timer.tc function that returns {<execution time in microseconds>, <result value>}. The reason that time is a macro is so that the entire expression remains unevaluated until called inside :timer.tc. To generate timing statistics over independent executions, :timer.tc and the expression are evaluated n times.

tim's People

Contributors

epfahl avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.