:@timed
@timed
A macro to execute an expression, and return the value of the expression, elapsed time, total bytes allocated, garbage collection time, and an object with various memory allocation counters.
Examples
julia> result, elapsed_time, bytes_allocated, gc_time, memory_counters = @timed begin
# Code block to be timed
# ...
end
This macro executes the provided code block and returns the value of the expression, elapsed time, total bytes allocated, garbage collection time, and an object with various memory allocation counters. Here are some examples of how to use the @timed macro:
-
Measure the execution time of a function:
julia> @timed begin function factorial(n) if n == 0 || n == 1 return 1 else return n * factorial(n - 1) end end factorial(10) end 3628800In this example, the
@timedmacro is used to measure the execution time of thefactorialfunction forn = 10. The return value of the expression is3628800.The output also includes the elapsed time, bytes allocated, garbage collection time, and memory counters.
-
Measure the execution time of a code block:
julia> @timed begin sum = 0 for i in 1:1000000 sum += i end endThis example measures the execution time of a code block that calculates the sum of numbers from 1 to 1,000,000. The return value of the expression is not used in this case.
The output includes the elapsed time, bytes allocated, garbage collection time, and memory counters.
-
Assign values returned by
@timedmacro:julia> result, elapsed_time, bytes_allocated, gc_time, memory_counters = @timed begin # Code block to be timed # ... endYou can assign the values returned by the
@timedmacro to individual variables for further analysis or processing.The
resultvariable will hold the value of the expression,elapsed_timewill hold the elapsed time in seconds,bytes_allocatedwill hold the total bytes allocated,gc_timewill hold the garbage collection time in seconds, andmemory_counterswill hold an object with various memory allocation counters.
Note: The @timed macro is useful for measuring the performance of code snippets and functions. It provides insights into the execution time and memory usage of the code being timed.
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.