# Check that compilation time = 0

**URL:** <https://discourse.julialang.org/t/check-that-compilation-time-0/101215>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 5, 2023, 2:31pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215 "2023-07-05T14:31:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [July 5, 2023, 2:31pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/1 "2023-07-05T14:31:09Z")

</div>

Hi,  
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

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 5, 2023, 3:11pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/2 "2023-07-05T15:11:25Z")

</div>

Not sure what it is that you want, but try JET.jl: [JuliaHub](https://juliahub.com/ui/Packages/JET/CNGCM/)

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [July 5, 2023, 3:18pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/3 "2023-07-05T15:18:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 5, 2023, 3:20pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/4 "2023-07-05T15:20:06Z")

</div>

> [@gitboy16](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 5, 2023, 3:25pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/5 "2023-07-05T15:25:10Z")

</div>

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
julia> @generated function slow_to_compile(::Type{T}) where {T}
           s = 0.0
           for i = 1:100_000_000
               s += 1.0
           end
           T(s)
       end
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)
1.0f8

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)
1.0e8

```

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.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 5, 2023, 3:28pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/6 "2023-07-05T15:28:29Z")

</div>

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

```julia
Base.cumulative_compile_timing(true)
t_start = Base.cumulative_compile_time_ns()
foo(args...) # run your code here
Base.cumulative_compile_timing(false)
t = Base.cumulative_compile_time_ns()[1] - t_start[1]
@test iszero(t)

```

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [July 5, 2023, 4:13pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/7 "2023-07-05T16:13:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [July 5, 2023, 4:15pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/8 "2023-07-05T16:15:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [July 5, 2023, 4:41pm UTC](https://discourse.julialang.org/t/check-that-compilation-time-0/101215/9 "2023-07-05T16:41:34Z")

</div>

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