I’m using @time
to time some code, what does it mean when it returns a negative value? What does it mean when it returns nothing
? What is the trusted resolution of @time
? I just need to get precision on the level of ~1s.
@time
does nothing more than get the current time, run your code, get the current time after that and subtracting the former from the latter (plus some GC/compiler statistics). You can see exactly what it does with @macroexpand
.
If it prints a negative time, it would mean that either your computer has its clock running backwards, or a datetime correction in the OS was performed while your code ran.
The timer itself guarantees (in current versions) millisecond resolution, as far as Im aware.
It’s like you have traveled in time The simplest expression
julia> @time 0
0.000001 seconds
even returns a positive result in my case. I have tried it for empty string and as well as nothing
. In the documentation of @time
it is stated that
│ Note
│
│ For more serious benchmarking, consider the @btime macro from the BenchmarkTools.jl package which among other
│ things evaluates the function multiple times in order to reduce noise.
and it is better to use @btime
macro for benchmarking issues.
Ok I found my mistake, and I feel really silly. My code was something like
time = @time blah()
and I assumed the time
variable would hold the time evaluated, but it holds the return value of the function blah
! Is there a way to get the result of @time into a variable?
@time
should never be able to report a time of nothing
. The actual return value of @time
is just the value of the expression and has nothing to do with the time it took to execute. That time is printed but never returned.
julia> a = @time 3-4
0.000001 seconds
-1
julia> a
-1
I suspect you’re trying to read the output of @time
into a variable. Perhaps you are looking for @elapsed
?
julia> b = @elapsed 3-4
5.0e-7 # this is how long the expression took to execute, in seconds