:@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:

  1. 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
    3628800

    In this example, the @timed macro is used to measure the execution time of the factorial function for n = 10. The return value of the expression is 3628800.

    The output also includes the elapsed time, bytes allocated, garbage collection time, and memory counters.

  2. Measure the execution time of a code block:

    julia> @timed begin
               sum = 0
               for i in 1:1000000
                   sum += i
               end
           end

    This 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.

  3. Assign values returned by @timed macro:

    julia> result, elapsed_time, bytes_allocated, gc_time, memory_counters = @timed begin
               # Code block to be timed
               # ...
           end

    You can assign the values returned by the @timed macro to individual variables for further analysis or processing.

    The result variable will hold the value of the expression, elapsed_time will hold the elapsed time in seconds, bytes_allocated will hold the total bytes allocated, gc_time will hold the garbage collection time in seconds, and memory_counters will 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.

*Required Field
Details

Checking you are not a robot: