why
Timing code execution
Hello,
I just ran the following code to time how long a part of a function takes to execute:
time1 = NaiveDateTime.utc_now
...some code or function call...
time2 = NaiveDateTime.utc_now
time_total = NaiveDateTime.diff(time2, time1, :microsecond)
time_total came back remarkably low (two-digit microseconds). Quite plausible (and fascinating!), but might I be missing something with this logic, particularly if a function call is between time1 and time2?
Thank you.
Most Liked Responses
NobbZ
Even though there are indeed benchmarking frameworks available, there is also :timer.tc/1,2,3 in Erlang.
For one time measurements, eg. for some progress report or sending to a metrics service, it’s usually sufficient.
tcoopman
If you want to do a micro benchmark (measure how long a function takes), doing one run of the function is not enough.
You can take a look at GitHub - bencheeorg/benchee: Easy and extensible benchmarking in Elixir providing you with lots of statistics! · GitHub to run microbenchmarks.








