# What is the cause of this performance difference between Julia and Cython?

**URL:** <https://discourse.julialang.org/t/what-is-the-cause-of-this-performance-difference-between-julia-and-cython/58886>\
**Category:** Performance\
**Tags:** question\
**Created:** [April 8, 2021, 10:59pm UTC](https://discourse.julialang.org/t/what-is-the-cause-of-this-performance-difference-between-julia-and-cython/58886 "2021-04-08T22:59:51Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 9, 2021, 1:17am UTC](https://discourse.julialang.org/t/what-is-the-cause-of-this-performance-difference-between-julia-and-cython/58886/4 "2021-04-09T01:17:52Z")

</div>

> [@mcabbott](#):
>
> ```julia
> julia> function compute_array_threads(m, n=m)
> x = Array{Int32}(undef, (m, n))
> @inbounds Threads.@threads for j = 0:n - 1
> for i = 0:m - 1
> x[i+1, j+1] = Int32(i*i + j*j)
> end
> end
> return x
> end;
> 
> ```

FYI, since `@threads` creates a closure and `@inbounds` does not penetrate closures, you need to put `@inbounds` inside `@threads` (as in `@inbounds x[i+1, j+1] = Int32(i*i + j*j)`).

The difference is probably very hard to observe for this kind of code, though. It’s easy to observe this with a bit heavier SIMD’able computation is indie. For example:

```julia
julia> function f!(ys ,xs)
           @inbounds Threads.@threads for i in eachindex(xs, ys)
               ys[i] = Base.FastMath.sqrt_fast(xs[i])
           end
       end;

julia> function g!(ys ,xs)
           Threads.@threads for i in eachindex(xs, ys)
               @inbounds ys[i] = Base.FastMath.sqrt_fast(xs[i])
           end
       end;

julia> xs = ones(2^25); ys = similar(xs);

julia> @btime f!(ys ,xs)
  13.981 ms (41 allocations: 3.64 KiB)

julia> @btime g!(ys ,xs)
  5.630 ms (41 allocations: 3.64 KiB)

julia> Threads.nthreads()
8

```

---

_[View the full topic](https://discourse.julialang.org/t/what-is-the-cause-of-this-performance-difference-between-julia-and-cython/58886)._
