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:
And here’s the output:
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!
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> 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:
-
@time
prints elapsed time (including GC time), but returns result of wrapped expression. E.g.result = @time A*B
returns a matrix. -
@elapsed
returns elapsed time. E.g.result = @elapsed A*B
returns time thatA * B
took. -
tic()
andtoc()
behave similar to@time
and seem to be borrowed from Matlab.
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.
time = @elapse res = f(x)
will save the result to res
and the time to time
.
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
(issue closed by a commit in September 2017)
Hi,
What @
sign means? Where to read about @something
signature? I’m very new to Julia