:@time
@time
A macro to execute an expression, printing the time it took to execute, the number of allocations, and the total number of bytes its execution caused to be allocated, before returning the value of the expression.
Examples
julia> @time sum(rand(10^6))
0.000048 seconds (9 allocations: 7.630 MiB)
5.000766101715013e5
This example uses the @time
macro to measure the execution time, allocations, and memory usage of the expression sum(rand(10^6))
. The output includes the time in seconds, the number of allocations, and the memory usage in bytes. Finally, it returns the value of the expression.
Common mistake example:
julia> @time for i in 1:10^6
i * i
end
0.000426 seconds (2.00 k allocations: 30.518 KiB)
In this example, the @time
macro is mistakenly used to measure the execution time of a loop. The output shows the time, allocations, and memory usage for the whole loop instead of just the expression inside the loop. It's important to understand that @time
measures the time and memory usage of a single expression, not a block of code.
See Also
:@time, :@timed, :@timev, now, sleep, tic, time, timedwait, Timer, time_ns, toc, toq,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.