How to benchmark @distributed block?

The following code works:

using Distributed

@time @distributed for i in 1:10000
    i * 2
end

  0.008885 seconds (8.50 k allocations: 492.944 KiB, 99.20% compilation time)
Task (done) @0x0000000109e9a6e0

But this causes my terminal to become unresponsive:

using BenchmarkTools
using Distributed

@btime @distributed for i in 1:10000
    i * 2
end

What is the right way to use @btime with @distributed?

(Julia Version 1.7.1 on M1 Mac)

What happens if you do for i in 1:3 ?

It also fails to run, crashing the REPL

In julia, it’s best practice to benchmark functions. So something like:

function bench()
  @distributed for i in 1:10000
    i * 2
  end
end
@btime bench()

would be better.

Thank you, that works.

I was wrong about @btime @distributed ... not working, it does run if I give it enough time. But it takes MUCH longer for the benchmark to run than @btime Threads.@threads ..., but the reported times are comparable, which is interesting. Thanks for the help!