# Tic() + toc/toq() VS @elapsed

**URL:** https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681
**Category:** New to Julia
**Created:** [January 25, 2017, 11:33am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681 "2017-01-25T11:33:41Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [October 12, 2019, 10:36am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/21 "2019-10-12T10:36:19Z")

</div>

A nice alternative is also [GitHub - KristofferC/TimerOutputs.jl: Formatted output of timed sections in Julia](https://github.com/KristofferC/TimerOutputs.jl), to time sections of a program, with nicely formated output about timings and more.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [October 13, 2019, 9:27am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/22 "2019-10-13T09:27:45Z")

</div>

> [@PetrKryslUCSD](#):
>
> Use `ts = time()` and `time() - ts`.

It seems the resolution I get from this is lower than `tic()` and `toq()` Julia used to have.  
Many operations I could measure with `tic()` and `toq()` are now show `0` run time.

Any idea?

## Update

In documentation of [`time()`](https://docs.julialang.org/en/v1/base/base/#Base.Libc.time-Tuple%7B%7D) it is written its Micro Seconds. Hence one should use higher accuracy timing as in [`time_ns()`](https://docs.julialang.org/en/v1/base/base/#Base.time_ns).

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [October 13, 2019, 9:49am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/23 "2019-10-13T09:49:04Z")

</div>

You can use `time_ns` instead.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [October 13, 2019, 10:30am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/24 "2019-10-13T10:30:52Z")

</div>

I think that as soon one tries to measure something on the computer that only takes a few microseconds, the result will be extremely noisy. Going down to nanoseconds will just magnify the problems.

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [October 13, 2019, 6:43pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/25 "2019-10-13T18:43:03Z")

</div>

Regarding `time_ns`, a potentially easier to understand variant is `rdtsc() = ccall("llvm.x86.rdtsc",llvmcall, Int, (), )`. Once you are below the microsecond range, you probably want to count CPU cycles instead. Also, that way you at least know where to ask/read for details (e.g. your CPU manual instead of having to figure out what `time_ns` actually does).

Direct measurement of small times is nontrivial. E.g. unqualified sentences like “this function takes 20ns to run” are meaningless: throughput? latency? In what context? Superscalar CPU don’t work by advancing from one instruction to the next. They can, at great cost, manufacture an illusion (“architected state”) of having sequentially gone through the steps described in your assembly code.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [October 13, 2019, 9:22pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/26 "2019-10-13T21:22:09Z")

</div>

Thank you for your answer.

I wanted to measure run time of a function.  
I ended up using `@elapsed` with running the same function for few times and taking the median.

Do you find it reasonable?

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [October 14, 2019, 1:07am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/27 "2019-10-14T01:07:50Z")

</div>

> [@RoyiAvital](#):
>
> I wanted to measure run time of a function.  
> I ended up using `@elapsed` with running the same function for few times and taking the median.
> 
> Do you find it reasonable?

Sure that is reasonable if the @elapsed is large enough to not care about millisecond overheads and includes whatever amount of garbage collection you need.

For faster functions, the @belapsed / @benchmark / @btime macros are handy. But these have all the other problems with running in loops.

The advantage of rdtsc over e.g. `time_ns` is only that it permits smaller loops / faster functions, because it has lower overhead and jitter, and cycles are imo easier to reason about than times. Benchmarking is still hard.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [October 14, 2019, 1:11am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/28 "2019-10-14T01:11:51Z")

</div>

> [@foobar\_lv2](#):
>
> The advantage of rdtsc over e.g. `time_ns` is only that it permits smaller loops / faster functions, because it has lower overhead and jitter, and cycles are imo easier to reason about than times. Benchmarking is still hard.

Yet if it counts cycles and the CPU’s changes its clock what’s the point? It will be accurate only for very small time frames where the CPU doesn’t change or in the steady state if we can guarantee the cooling solution is good enough.

[Previous page](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681.md?page=1)
