Why is there a large range in evaluation time when using map?

When I was trying to practice benchmarking some code, I ran into an unexpected result.

I ran

@benchmark map(x -> x^2, 1:10^6)

and obtained the following results.

BenchmarkTools.Trial: 3230 samples with 1 evaluation.
 Range (min … max):  512.863 μs … 27.653 ms  ┊ GC (min … max):  0.00% … 96.25%
 Time  (median):     860.196 μs              ┊ GC (median):     0.00%
 Time  (mean ± σ):     1.545 ms ±  2.938 ms  ┊ GC (mean ± σ):  45.57% ± 22.36%

  ▃█                                                            
  ██▄▄▁▁▁▁▁▁▅▃▃▄▃▁▁▁▃▃▃▄▃▅▄▅▅▅▅▅▄▆▄▅▅▆▇▆▆▅▇▄▅▄▁▄▄▅▅▄▄▄▄▁▃▅▄▄▅▄ █
  513 μs        Histogram: log(frequency) by time      16.6 ms <

Could anyone explain why there is such a large range between the slowest and fastest evaluation times (512.863 μs … 27.653 ms)?

I would guess garbage collection. It doesn’t need to trigger every run, so sometimes it takes 0% of your runtime, but sometimes it takes 96%, according to the benchmark outputs here.

5 Likes