Execution time and measurement

Intermediate Julia

Anthony Markham

Quantitative Developer

Initial function definition

  • While problems have multiple solutions, there is often an 'optimal' solution
  • Large memory usage and long execution times can frustrate both users and developers
  • Loops are a common cause of longer execution times and inefficiency
function my_function()
  x = Vector{Char}()
  for i in "Anthony"
    push!(x, i)
  end
  println(x)
end
['A', 'n', 't', 'h', 'o', 'n', 'y']
Intermediate Julia

Base @time macro

  • We use macros to time our functions.

Advantages:

  • Part of Julia's base package
  • Simple and easy to call
  • Output is easy to interpret

Disadvantages:

  • Limited flexibility
  • Only times the function once
  • First macro call has compilation overhead
  • Call the macro on our function
@time my_function()
@time my_function()
  • Return values

0.278266 seconds (110.66 k allocations: 6.343 MiB, 99.79% compilation time)

0.000493 seconds (100 allocations: 3.281 KiB)

Intermediate Julia

BenchmarkTools - @benchmark

Why use @benchmark over @time?

  • The most flexible option, lots of parameters to tweak, such as number of samples.
  • In-depth runtime statistics (range, median, mean).
@benchmark my_function

Intermediate Julia

Let's practice!

Intermediate Julia

Preparing Video For Download...