Get startedGet started for free

Timing a function

Timing our code is critical as slow code can provide a poor experience for a user, or a script could be running far longer and less efficiently than it should. One of the major benefits that we want to take advantage of in Julia is its speed, so being able to accurately benchmark your code is even more important.

Now that you've seen the different options that are available for benchmarking, let's try it ourselves with some examples.

In the first step, use the base package @time macro to time the function my_function. In the second step, use the BenchmarkTools package to time the same function my_function.

We have already imported the BenchmarkTools package for you.

This exercise is part of the course

Intermediate Julia

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# This function will square each number from 1 to 10 and push it to a vector
function my_function()
  x = Vector{Int}()
  for i in 1:10
    push!(x, i^2)
  end
  return println(x)
end

# Time my_function using the base time macro
____ my_function()
Edit and Run Code