Ideas for examples to demonstrate precompilation time and runtime performance?

I am looking for some ideas for short example codes which can be used to demonstrate how much time it takes for Julia to compile when code is first run vs the time taken for code to execute after it has been run once (and hence compiled).

There is a question on Stack Overflow about this. I provide the link here.

However, on modern machines, it appears that this takes less than 0.000000 seconds to compile and run. Hence the demo doesn’t work on a fast modern CPU.

Does anyone have an idea for some relatively simple code which could be used to demonstrate the difference between running

  • the first time (which includes compilation)
  • subsequent runs (where the code has previously been compiled)

I am looking to demonstrate a few things:

  • How much faster a function can be made using __precompile__()
  • How much faster a function can be made by using __precompile__() and running the function by calling it (see example below: # precompile hints)
__precompile__()

module TestPackage

export cube

square(x) = x * x
cube(x) = x * square(x)

# precompile hints
cube(0)

end

I appreciate this is a bit of a vague or open-ended question. Whatever is being compiled needs to be simple enough to be easy to understand but complex enough to actually spend a reasonable amount of time (maybe ~ 0.01 sec) compiling.

Any suggestions gratefully received. I’m sort of hoping someone already has done this experiment and can provide some relatively minimal-effort-required pre-made example.

A simple sum function seems to fulfil your criteria, at least on my pc:

julia> function mysum(v::AbstractVector{T}) where T
           s = zero(T)
           for x in v
               s += x
           end
           return s
       end
mysum (generic function with 1 method)

julia> v = rand(1000);

julia> @time mysum(v)
  0.008318 seconds (2.14 k allocations: 138.344 KiB, 99.74% compilation time)
487.44084632309904

julia> @time mysum(v)  # Edit: v not x
  0.000004 seconds (1 allocation: 16 bytes)
487.44084632309904

If you put it in a __precompile__() module like yours, the timing for the ‘first’ run is like the second run here. (It’s would probably be better to use PrecompileTools.jl though.)

1 Like

Is this second call

julia> @time mysum(x)

supposed to be

julia> @time mysum(v)

?

Indeed, my bad.
(I originally used x = rand(1000), but decided to rename it to v for consistency with the variables used in mysum.)

1 Like

Ok cool thanks for clarification - I’ve posted a new related, but separate, question, feel free to have a look if you are interested