Skip to content

Conversation

@IanButterworth
Copy link
Member

@NHDaly, in thinking about #39760 I wondered if this might be an alternative way to achieve what #37938 is doing?

Opening as a draft given I'm not sure whether it's better!

It only reports top-level compilation time if it exceeds 0.5 seconds, and it's intentionally low-precision, given higher precision would seem odd given the 0.5 second cutoff.
If users are trying to time with @time and care about top-level compilation < 0.5 seconds they probably want to be using @btime anyway.

julia> @eval function f(a)  # Expensive function to compile. (Thanks @yuyichao!)
                  $(quote $([:(a = a + a * 10 + (a > 0 ? $i : 0.2)) for i in 1:401]...) end)
                  $(quote $([:(a = a + a * 10 + (a > 0 ? $i : 0.2)) for i in 1:401]...) end)
                  return a
              end
f (generic function with 1 method)

julia> @time f(2)
  0.000000 seconds + 3.0 seconds of top-level compilation
-Inf

julia> x = 1
1

julia> @time f(x)
 19.168382 seconds (527.44 k allocations: 19.444 MiB, 100.00% compilation time)
Inf

julia> @time f(x)
  0.000015 seconds (1 allocation: 16 bytes)
Inf

julia> for i in 1:4
       @time sleep(1)
       end
  1.002893 seconds (4 allocations: 128 bytes)
  1.004821 seconds (4 allocations: 128 bytes)
  1.005353 seconds (4 allocations: 128 bytes)
  1.004838 seconds (4 allocations: 128 bytes)

julia> Threads.@threads for i in 1:4
       @time sleep(1)
       end
  1.005255 seconds (42 allocations: 2.438 KiB)
  1.005194 seconds (4 allocations: 128 bytes)
  1.005236 seconds (30 allocations: 2.125 KiB)
  1.005329 seconds (83 allocations: 5.562 KiB)

If moved forward, will need working into other timing macros

"""
macro time(ex)
if Threads.threadid() == 1 && latest_time_cumucomptime[] == 0 # catch the first time `@time` is interpreted
latest_time_cumucomptime[] = cumulative_compile_time_ns_before()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Macro expansion should not have side effects, so this is not a great way to implement this.

@IanButterworth
Copy link
Member Author

Closing as deemed to be a bad approach, and the @time @eval trick added to the docs in 1.6 is a helpful tool for investigating this issue

@IanButterworth IanButterworth deleted the ib/time_include_topleveloverhead branch April 4, 2021 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants