@@ -55,6 +55,8 @@ function gc_alloc_count(diff::GC_Diff)
55
55
diff. malloc + diff. realloc + diff. poolalloc + diff. bigalloc
56
56
end
57
57
58
+ # cumulative total time spent on compilation
59
+ cumulative_compile_time_ns () = ccall (:jl_cumulative_compile_time_ns , UInt64, ())
58
60
59
61
# total time spend in garbage collection, in nanoseconds
60
62
gc_time_ns () = ccall (:jl_gc_total_hrtime , UInt64, ())
@@ -102,7 +104,7 @@ function format_bytes(bytes) # also used by InteractiveUtils
102
104
end
103
105
end
104
106
105
- function time_print (elapsedtime, bytes= 0 , gctime= 0 , allocs= 0 )
107
+ function time_print (elapsedtime, bytes= 0 , gctime= 0 , allocs= 0 , compile_time = 0 )
106
108
timestr = Ryu. writefixed (Float64 (elapsedtime/ 1e9 ), 6 )
107
109
length (timestr) < 10 && print (" " ^ (10 - length (timestr)))
108
110
print (timestr, " seconds" )
@@ -118,15 +120,18 @@ function time_print(elapsedtime, bytes=0, gctime=0, allocs=0)
118
120
if gctime > 0
119
121
print (" , " , Ryu. writefixed (Float64 (100 * gctime/ elapsedtime), 2 ), " % gc time" )
120
122
end
123
+ if compile_time > 0
124
+ print (" , " , Ryu. writefixed (Float64 (100 * compile_time/ elapsedtime), 2 ), " % compilation time" )
125
+ end
121
126
if bytes != 0 || allocs != 0
122
127
print (" )" )
123
128
end
124
129
nothing
125
130
end
126
131
127
- function timev_print (elapsedtime, diff:: GC_Diff )
132
+ function timev_print (elapsedtime, diff:: GC_Diff , compile_time )
128
133
allocs = gc_alloc_count (diff)
129
- time_print (elapsedtime, diff. allocd, diff. total_time, allocs)
134
+ time_print (elapsedtime, diff. allocd, diff. total_time, allocs, compile_time )
130
135
print (" \n elapsed time (ns): $elapsedtime \n " )
131
136
padded_nonzero_print (diff. total_time, " gc time (ns)" )
132
137
padded_nonzero_print (diff. allocd, " bytes allocated" )
144
149
145
150
A macro to execute an expression, printing the time it took to execute, the number of
146
151
allocations, and the total number of bytes its execution caused to be allocated, before
147
- returning the value of the expression.
152
+ returning the value of the expression. Any time spent garbage collecting (gc) or
153
+ compiling is shown as a percentage.
148
154
149
155
See also [`@timev`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and
150
156
[`@allocated`](@ref).
@@ -155,8 +161,13 @@ See also [`@timev`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and
155
161
reduce noise.
156
162
157
163
```julia-repl
158
- julia> @time rand(10^6);
159
- 0.001525 seconds (7 allocations: 7.630 MiB)
164
+ julia> x = rand(10,10);
165
+
166
+ julia> @time x * x;
167
+ 0.606588 seconds (2.19 M allocations: 116.555 MiB, 3.75% gc time, 99.94% compilation time)
168
+
169
+ julia> @time x * x;
170
+ 0.000009 seconds (1 allocation: 896 bytes)
160
171
161
172
julia> @time begin
162
173
sleep(0.3)
@@ -170,12 +181,14 @@ macro time(ex)
170
181
quote
171
182
while false ; end # compiler heuristic: compile this block (alter this if the heuristic changes)
172
183
local stats = gc_num ()
184
+ local compile_elapsedtime = cumulative_compile_time_ns ()
173
185
local elapsedtime = time_ns ()
174
186
local val = $ (esc (ex))
175
187
elapsedtime = time_ns () - elapsedtime
188
+ compile_elapsedtime = cumulative_compile_time_ns () - compile_elapsedtime
176
189
local diff = GC_Diff (gc_num (), stats)
177
190
time_print (elapsedtime, diff. allocd, diff. total_time,
178
- gc_alloc_count (diff))
191
+ gc_alloc_count (diff), compile_elapsedtime )
179
192
println ()
180
193
val
181
194
end
@@ -192,22 +205,36 @@ See also [`@time`](@ref), [`@timed`](@ref), [`@elapsed`](@ref), and
192
205
[`@allocated`](@ref).
193
206
194
207
```julia-repl
195
- julia> @timev rand(10^6);
196
- 0.001006 seconds (7 allocations: 7.630 MiB)
197
- elapsed time (ns): 1005567
198
- bytes allocated: 8000256
199
- pool allocs: 6
200
- malloc() calls: 1
208
+ julia> x = rand(10,10);
209
+
210
+ julia> @timev x * x;
211
+ 0.546770 seconds (2.20 M allocations: 116.632 MiB, 4.23% gc time, 99.94% compilation time)
212
+ elapsed time (ns): 546769547
213
+ gc time (ns): 23115606
214
+ bytes allocated: 122297811
215
+ pool allocs: 2197930
216
+ non-pool GC allocs:1327
217
+ malloc() calls: 36
218
+ realloc() calls: 5
219
+ GC pauses: 3
220
+
221
+ julia> @timev x * x;
222
+ 0.000010 seconds (1 allocation: 896 bytes)
223
+ elapsed time (ns): 9848
224
+ bytes allocated: 896
225
+ pool allocs: 1
201
226
```
202
227
"""
203
228
macro timev (ex)
204
229
quote
205
230
while false ; end # compiler heuristic: compile this block (alter this if the heuristic changes)
206
231
local stats = gc_num ()
232
+ local compile_elapsedtime = cumulative_compile_time_ns ()
207
233
local elapsedtime = time_ns ()
208
234
local val = $ (esc (ex))
209
235
elapsedtime = time_ns () - elapsedtime
210
- timev_print (elapsedtime, GC_Diff (gc_num (), stats))
236
+ compile_elapsedtime = cumulative_compile_time_ns () - compile_elapsedtime
237
+ timev_print (elapsedtime, GC_Diff (gc_num (), stats), compile_elapsedtime)
211
238
val
212
239
end
213
240
end
0 commit comments