# How to benchmark @distributed block?

**URL:** <https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973>\
**Category:** General Usage\
**Tags:** parallel, distributed\
**Created:** [July 8, 2022, 11:49pm UTC](https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973 "2022-07-08T23:49:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![v715](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v715/32/37202_2.png) [@v715](https://discourse.julialang.org/u/v715)\
**Post date:** [July 8, 2022, 11:49pm UTC](https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973/1 "2022-07-08T23:49:22Z")

</div>

The following code works:

```julia
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:

```julia
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)

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [July 9, 2022, 8:11pm UTC](https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973/2 "2022-07-09T20:11:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![v715](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v715/32/37202_2.png) [@v715](https://discourse.julialang.org/u/v715)\
**Post date:** [July 9, 2022, 9:45pm UTC](https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973/3 "2022-07-09T21:45:34Z")

</div>

It also fails to run, crashing the REPL

---

<div class="post-metadata">

**Author:** ![rejuvyesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rejuvyesh/32/91_2.png) [@rejuvyesh](https://discourse.julialang.org/u/rejuvyesh)\
**Post date:** [July 9, 2022, 10:06pm UTC](https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973/4 "2022-07-09T22:06:44Z")

</div>

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

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

```

would be better.

---

<div class="post-metadata">

**Author:** ![v715](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v715/32/37202_2.png) [@v715](https://discourse.julialang.org/u/v715)\
**Post date:** [July 9, 2022, 10:37pm UTC](https://discourse.julialang.org/t/how-to-benchmark-distributed-block/83973/5 "2022-07-09T22:37:23Z")

</div>

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!
