# 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:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [January 25, 2017, 11:33am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/1 "2017-01-25T11:33:41Z")

</div>

Other than the difference in syntax, is there any essential difference between a combination of tic+toc and @elapsed? Any difference between what they return? The only difference described by the doc is as follows, which I don’t quite understand. Can anyone explain a bit?

> instead returning the number of seconds it (i.e. @elapsed) took to execute as a floating-point number.

Thanks a lot!!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 25, 2017, 12:07pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/2 "2017-01-25T12:07:55Z")

</div>

The all use the same basic functionality (see `time_ns()`), but provide a different user interface. `@time` displays the time (and other info), use `@elapsed` when instead of displaying it, you would like to use the value, eg to compile statistics, etc, but see

> **[GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia...](https://github.com/JuliaCI/BenchmarkTools.jl)**
>
> A benchmarking framework for the Julia language. Contribute to JuliaCI/BenchmarkTools.jl development by creating an account on GitHub.

`tic` and `toc` save the info in the global state (of the task). I find `@time` and `@elapsed` cleaner, but YMMV. Also, it is very easy to implement a combination of `@elapsed` and `@time`, if for some reason you need both the time and the value (eg as a tuple).

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [January 25, 2017, 2:43pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/3 "2017-01-25T14:43:09Z")

</div>

In addition to `@time`, as @Tamas_Papp mentioned, there is also `@timev`, which displays even more detailed information (about memory allocations, the GC, etc.), that can help if you are trying to figure out a performance issue.

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [January 25, 2017, 5:51pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/4 "2017-01-25T17:51:17Z")

</div>

> [@Tamas\_Papp](#):
>
> The all use the same basic functionality (see time\_ns()), but provide a different user interface… [redacted]

> [@ScottPJones](#):
>
> In addition to @time, as @Tamas_Papp mentioned, there is also @timev, which displays even more detailed information (about memory allocations, the GC, etc.), that can help if you are trying to figure out a performance issue.

Thanks both of you for the suggestions. Now I see the difference. I have been using BenchmarkTools.jl too to have more detailed statistics. However, if I want to use `@elapsed` or `@benchmark`, I have to wrap up all I want to time in a function. That’s not very convenient. That’s why I think I have to resort to using tic() and toc() sometimes.

May I know what most of you do when trying to time a few lines of your code in a big project or a large module? Do you also wrap the lines you want to time in a function in order to use `@elapsed` or `@benchmark`? Thanks!

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 25, 2017, 6:05pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/5 "2017-01-25T18:05:50Z")

</div>

I use @benchmark to time a single call. BenchmarkTools is much more careful about compensating for externalities than are the other options.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 25, 2017, 6:09pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/6 "2017-01-25T18:09:28Z")

</div>

You **do** want to wrap things in a function for [benchmarking](http://docs.julialang.org/en/latest/manual/performance-tips.html#Measure-performance-with-%5B@time%5D(@ref)-and-pay-attention-to-memory-allocation-1). But otherwise you could use

```julia
@time begin
    ...
end

```

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [January 26, 2017, 10:57am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/7 "2017-01-26T10:57:27Z")

</div>

Thanks. For a standalone function without much of dependencies, I use `BenchmarkTools.jl` too. What do you use for timing a few lines of code in a project?

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [January 26, 2017, 11:00am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/8 "2017-01-26T11:00:00Z")

</div>

Thanks. This is very useful. One question - What is the breakdown of the time returned by `@time`? My understanding is the time returned by `@time` = compiling time + memory allocation time + computing time + GC time. Is it right?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 26, 2017, 11:46am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/9 "2017-01-26T11:46:42Z")

</div>

_Never_ do this in global scope, though – the result is worse than meaningless. (I.e. it is actively misleading.)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 26, 2017, 1:01pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/10 "2017-01-26T13:01:44Z")

</div>

I don’t think so. First, `@time` just _returns_ the result of evaluating the expression, not the elapsed time. That is displayed. At least on `0.5`, it _prints_ the elapsed time and the allocations (as a count, and the total memory allocated).

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 26, 2017, 1:06pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/11 "2017-01-26T13:06:18Z")

</div>

For quick & dirty timing I use `@time`, **twice** , wrapping things in a function. I only care about the second result. I do this realizing that there could easily be ±20% sampling errors.

For anything more serious, I use `BenchmarkTools.jl`. Which is a great tool. For very little time investment (learning how to use it), you get very nice & consistent benchmarks. For something quick, I use `seconds=3` to get three seconds worth of samples.

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [January 26, 2017, 1:08pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/12 "2017-01-26T13:08:23Z")

</div>

> [@Tamas\_Papp](#):
>
> That is displayed.

What is displayed? Can you elaborate a bit?

> [@Tamas\_Papp](#):
>
> For quick & dirty timing I use @time, twice, wrapping things in a function. I only care about the second result. I do this realizing that there could easily be ±20% sampling errors.
> 
> For anything more serious, I use BenchmarkTools.jl. Which is a great tool. For very little time investment (learning how to use it), you get very nice & consistent bench.marks. For something quick, I use seconds=3 to get three seconds worth of samples.

Thanks!! This is very helpful

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 26, 2017, 1:20pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/13 "2017-01-26T13:20:41Z")

</div>

> [@chobbes](#):
>
> What is displayed? Can you elaborate a bit?

The elapsed time (see second line below):

```julia
julia> x = @time 1+1
  0.000007 seconds (4 allocations: 160 bytes)
2

julia> x
2

```

which also demonstrates that the value of `@time expr` is the value of `expr`.

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [January 26, 2017, 1:24pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/14 "2017-01-26T13:24:37Z")

</div>

> [@Tamas\_Papp](#):
>
> which also demonstrates that the value of @time expr is the value of expr.

Thanks. Well. I thought you something else. Now I understand what you meant.

Can you give a breakdown of the value returned by @elapsed? I’m sorry. I’m really curious what may contribute to the total returned by @elapsed. Thanks!!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 26, 2017, 1:41pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/15 "2017-01-26T13:41:49Z")

</div>

`@elapsed` just returns a single value, which, AFAIK, is wall time spent executing the expression (because `time_ns()` counts that). It is a very simple macro. You can read the source, eg using `@edit @elapsed 1` or something like that.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 26, 2017, 2:07pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/16 "2017-01-26T14:07:36Z")

</div>

```
using BenchmarkTools

macro nanosecs(xs...)
    quote
        bench = @benchmark $(esc(xs...))
        t0 = bench.times[1]
        t1 = median(bench.times)
        tm = round(Int64, 2/(1/t0 + 1/t1))
        return tm
    end
end

macro memused(xs...)
    quote
        bench = @benchmark $(esc(xs...))
        a = bench.allocs
        m = bench.memory
        return (m,a)
    end
end

# use values held in variables
a = 0.75
@nanosecs sin(a)
# Int64 harmonic mean of fastest and median 
@memused sin(a)
# total bytes, distinct allocations

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 26, 2017, 4:38pm UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/17 "2017-01-26T16:38:44Z")

</div>

> [@Tamas\_Papp](#):
>
> For anything more serious, I use BenchmarkTools.jl. Which is a great tool. For very little time investment (learning how to use it), you get very nice & consistent benchmarks. For something quick, I use seconds=3 to get three seconds worth of samples.

Note that BenchmarkTools now has `@btime` and `@belapsed`, which are drop-in replacements for `@time` and `@elapsed`. (They take longer to run, however, because they spend some time collecting timing statistics, whereas `@time` and `@elapsed` only run the code once.)

---

<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 12, 2019, 7:34am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/18 "2019-10-12T07:34:45Z")

</div>

Are there **functions** which replace `tic()` and `toc()`?

I want to calculate the time difference manually.

---

<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 12, 2019, 7:36am UTC](https://discourse.julialang.org/t/tic-toc-toq-vs-elapsed/1681/19 "2019-10-12T07:36:28Z")

</div>

Use `ts = time()` and `time() - ts`.

---

<div class="post-metadata">

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

</div>

![ticktock](https://global.discourse-cdn.com/julialang/original/3X/d/d/dd111a054fd313725f2762bde8af5464ec67f44d.gif)

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