Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

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} =, :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., :puts, ["Hello World"])
# {22, :ok}

You can also call with a function and args:

adding = fn (x, y) ->  x + y end, [1,3])
# {5, 4}

Or just a function: -> 
    # something really expensive
# {1_302_342, :ok}
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.