# Difference between tic() toc(), @time or @elapse in Julia?

**URL:** <https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 28, 2016, 8:53am UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177 "2016-12-28T08:53:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![may811204](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@may811204](https://discourse.julialang.org/u/may811204)\
**Post date:** [December 28, 2016, 8:53am UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/1 "2016-12-28T08:53:05Z")

</div>

Hi, I am new to Julia and I am wondering what’s the difference between tic() toc(),@time, @elapse when measuring elapsed time in Julia.  
I tried the codes below:  
 ![](https://global.discourse-cdn.com/julialang/original/3X/6/2/62a0829aecc43e0ede545c0621798a5bc7c8a735.png)  
And here’s the output:  
 ![](https://global.discourse-cdn.com/julialang/original/3X/9/e/9ebcf47003c6e99f24f4ce091c7e5ba8b140019f.png)  
It showed that tic() toc() and t2-t1 share relatively close quantity. On contrast, @time and @elapse are close. However, there is difference between tic toc and @time.  
What is the difference and why?  
Which approach should I use to measure the elapsed time?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [December 28, 2016, 1:15pm UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/2 "2016-12-28T13:15:02Z")

</div>

You can see what’s inside of each of these calls using `macroexpand` (for `@time` and `@elapsed`) or `@edit` (for `tic()`/`toc()`). E.g. for `@elapsed`:

```julia
julia> macroexpand(:(@elapsed A * B))
quote # util.jl, line 225:
    local #5#t0 = (Base.time_ns)() # util.jl, line 226:
    local #6#val = A * B # util.jl, line 227:
    ((Base.time_ns)() - #5#t0) / 1.0e9
end

```

A couple of differences that I noticed:

1. `@time` prints elapsed time (including GC time), but returns result of wrapped expression. E.g. `result = @time A*B` returns a matrix.
2. `@elapsed` returns elapsed time. E.g. `result = @elapsed A*B` returns time that `A * B` took.
3. `tic()` and `toc()` behave similar to `@time` and seem to be borrowed from [Matlab](https://www.mathworks.com/help/matlab/ref/tic.html?requestedDomain=www.mathworks.com).

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 21, 2017, 12:02am UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/3 "2017-10-21T00:02:37Z")

</div>

Is there a way to return both the result and the timing? I am trying to build a benchmarking suite for data manipulation, so my use case is generating a large dataframe and keep the result but also keep track of how long it takes to create that dataframe.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 21, 2017, 12:25am UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/4 "2017-10-21T00:25:31Z")

</div>

`time = @elapse res = f(x)` will save the result to `res` and the time to `time`.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [June 11, 2018, 1:46pm UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/5 "2018-06-11T13:46:17Z")

</div>

For the reference, I believe that `tic` and `toc` are getting deprecated in Julia 0.7, cf. issue #17046:

[https://github.com/JuliaLang/julia/issues/17046](https://github.com/JuliaLang/julia/issues/17046)

(issue closed by a commit in September 2017)

---

<div class="post-metadata">

**Author:** ![kerim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kerim/32/37844_2.png) [@kerim](https://discourse.julialang.org/u/kerim)\
**Post date:** [June 30, 2021, 11:36pm UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/6 "2021-06-30T23:36:09Z")

</div>

Hi,

What `@` sign means? Where to read about `@something` signature? I’m very new to Julia

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 1, 2021, 12:07am UTC](https://discourse.julialang.org/t/difference-between-tic-toc-time-or-elapse-in-julia/1177/7 "2021-07-01T00:07:28Z")

</div>

[https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros)
