# Problem on benchmarking multi-thread code

**URL:** https://discourse.julialang.org/t/problem-on-benchmarking-multi-thread-code/55021
**Category:** Performance
**Created:** [February 10, 2021, 7:56pm UTC](https://discourse.julialang.org/t/problem-on-benchmarking-multi-thread-code/55021 "2021-02-10T19:56:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [February 10, 2021, 7:56pm UTC](https://discourse.julialang.org/t/problem-on-benchmarking-multi-thread-code/55021/1 "2021-02-10T19:56:14Z")

</div>

Hello,  
I am trying to benchmark function with thread parallelization and compare the result with non-parallelized version of function.

```julia
using Base.Threads

nx = 10000
a = zeros(nx)
b = rand(nx)
dindicies = 2:(nx-1)

function update!(A,B)
    for i=dindicies
        @inbounds A[i] = B[i] - B[i-1]
    end
end

function update_thread!(A,B)
    @threads for i=dindicies
        @inbounds A[i] = B[i] - B[i-1]
    end
end

```

I get the result with @btime

```julia
julia> @btime update!($a,$b)
  1.329 ms (68455 allocations: 1.20 MiB)

```

```julia
julia> @btime update_thread!($a,$b)
  5.796 μs (21 allocations: 2.81 KiB)

```

Why the non-parallelized version allocate far more memory while the parallelized version doesn’t?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 10, 2021, 8:04pm UTC](https://discourse.julialang.org/t/problem-on-benchmarking-multi-thread-code/55021/2 "2021-02-10T20:04:35Z")

</div>

Your functions use global variables. That might not be optimal.

---

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [February 10, 2021, 8:21pm UTC](https://discourse.julialang.org/t/problem-on-benchmarking-multi-thread-code/55021/3 "2021-02-10T20:21:27Z")

</div>

I still not quite understand. Could you give me more hint? What make me confused is the big difference of allocations.

---

<div class="post-metadata">

### Author: ![moeddel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moeddel/32/18641_2.png) [@moeddel](https://discourse.julialang.org/u/moeddel)
#### Post date: [February 10, 2021, 8:27pm UTC](https://discourse.julialang.org/t/problem-on-benchmarking-multi-thread-code/55021/4 "2021-02-10T20:27:01Z")

</div>

Try to declare all variables used within the function scope

```julia
function update!(A,B)
    nx = 10000
    dindicies = 2:(nx-1)
    for i=dindicies
        @inbounds A[i] = B[i] - B[i-1]
    end
end

```

See [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables) for more details.
