Is there a macro like 'time' in Clojure?

Hello, I’m a beginner of Elixir.
I wonder if there is a macro like ‘time’ in Clojure.
It can tell the time our function took.

user> (time (Thread/sleep 100))
"Elapsed time: 100.284772 msecs"
nil

https://clojuredocs.org/clojure.core/time
Maybe it’s convenient when optimizing our functions.

some suggestions here:

2 Likes

For actual benchmarking with proper statistical analysis I’d recommend using Benchee.

1 Like

@OvermindDL1 I think the point would be simple built in timing “without bringing in a dependency to my project” .

In the linked SO post, I asked if this could be a simple addition to IEx.

Sadly though a simple time function is not very accurate and can return fairly wildly different values until a few things have stabilized, including setting up the GC well, testing overhead, etc… I’m not a fan of simple time calls like that because of reasons like that. They work better in languages like Ruby or Python where there is no JIT, but JIT’s induce an unexpected amount of changes until stabilization happens (and I don’t remember if the BEAM/HiPE is a AOT JIT or a Tracing JIT or etc…)…

AOT, there is not runtime JIT on BEAM for now. It is in the backlog/roadmap of the OTP team for a long time, but it is quite complex, would be a huge engineering work and… There is just no real big user need for it for Ericsonn

1 Like

HiPE is the JIT for erlang (AOT it looks), it does not work on Windows though and although it can speed up some things it can also cause some things to be slower, it is more of a fine-tuning thing to turn on per-group of files (calling between the BEAM and HiPE modules also incurs a speed hit). ^.^

Just for note for others passing by. :slight_smile:

To answer the question, there’s a simple function in erlang to accomplish this:

{time, value} = :timer.tc(fn -> do_some_stuff() end)

It returns time in microseconds:
http://erlang.org/doc/man/timer.html#tc-1

2 Likes