Check that compilation time = 0

When I use @time, Julia displays the number of allocation, gc time, compilation time, (recompilation time I think?)…
Is there a way to test that the compilation time is equal to 0, i.e. that no compilation happens?
I am trying to remove any compilation from my code and I managed to do it with tools like PrecompileTools…etc. I woukd like to make sure that whoever change the code does not introduce any compilation when running some function in the package.
Thank you

Not sure what it is that you want, but try JET.jl: JuliaHub

Sorry maybe I am not clear. JET.jl is not what I am looking for. I am looking for a way to test (with code) whether compilation time equal 0 or not when I run @time mycode.

Does this refer to the first time you run @time mycode in a fresh Julia session? Or do you mean the subsequent runs?

Note that @time tries to precompile your function before calling it.
Thus @time @eval is needed when you want to measure actual compile times:

julia> @generated function slow_to_compile(::Type{T}) where {T}
           s = 0.0
           for i = 1:100_000_000
               s += 1.0
slow_to_compile (generic function with 1 method)

julia> @time @eval @time slow_to_compile(Float32)
  0.000000 seconds
  0.116136 seconds (8.44 k allocations: 561.078 KiB, 99.00% compilation time)

julia> @time @eval @time slow_to_compile(Float64)
  0.000001 seconds
  0.110334 seconds (6.56 k allocations: 441.352 KiB, 98.93% compilation time)

However, just @time foo(...) without the @eval is probably fine here, unless you literally need 0 compilation time, because @time removing all compile time requires your function was thoroughly inferred from that top level call, which is a pretty good place to be.


Look at @macroexpand @time 1+2 to see how it measures compile times:

t_start = Base.cumulative_compile_time_ns()
foo(args...)  # run your code here
t = Base.cumulative_compile_time_ns()[1] - t_start[1]
@test iszero(t)
1 Like

To the first time I run the code. If I did a good at precompiling the function, then compilation time = 0 (that is what I want to test) otherwise if it shows a compilation time then I did not do my job correctly.

1 Like

Looks like it is doing the trick even if I don’t fully understand what is going on. Although you need to do a dry run without the code to test before. Do you agree?

Ideally I would have a function or a macro that would tell me whether the code I am running has compile time or not (return true or false).