# Julia can be dramatically slower than Matlab when solving 2D PDE

**URL:** <https://discourse.julialang.org/t/julia-can-be-dramatically-slower-than-matlab-when-solving-2d-pde/122321>\
**Category:** New to Julia\
**Tags:** performance, matlab, pde\
**Created:** [November 6, 2024, 8:46am UTC](https://discourse.julialang.org/t/julia-can-be-dramatically-slower-than-matlab-when-solving-2d-pde/122321 "2024-11-06T08:46:27Z")\
**Posts on this page:** 3\
**Page:** 3

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [November 9, 2024, 11:10am UTC](https://discourse.julialang.org/t/julia-can-be-dramatically-slower-than-matlab-when-solving-2d-pde/122321/42 "2024-11-09T11:10:41Z")

</div>

The rhp of this one will be `Float64` if any of the values is `Float64`:

```julia
T[2:N-1,2:N-1] = Tn[2:N-1,2:N-1] + Fo * dt * ((Tn[3:N,2:N-1] - 2 * Tn[2:N-1,2:N-1] + Tn[1:N-2,2:N-1]) / h + (Tn[2:N-1,3:N] - 2 * Tn[2:N-1,2:N-1] + Tn[2:N-1,1:N-2]) / h)

```

Also, I’m not sure how CUDA likes slices.  
One additional tip in their documentation is to avoid `Int64`, so maybe it’ll be beneficial to also replace factors `2` by `Int32(2)`.

> [@Alex90](#):
>
> Unfortunately, i did not understand how to run it properly.

It must be the same as previously, the only change is that I removed `N` as explicit parameter and it is calculated from the size of the array.

---

<div class="post-metadata">

**Author:** ![Alex90](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Alex90](https://discourse.julialang.org/u/Alex90)\
**Post date:** [November 9, 2024, 2:05pm UTC](https://discourse.julialang.org/t/julia-can-be-dramatically-slower-than-matlab-when-solving-2d-pde/122321/43 "2024-11-09T14:05:57Z")

</div>

Got it. Thanks!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 9, 2024, 2:25pm UTC](https://discourse.julialang.org/t/julia-can-be-dramatically-slower-than-matlab-when-solving-2d-pde/122321/44 "2024-11-09T14:25:33Z")

</div>

> [@Vasily\_Pisarev](#):
>
> Also, I’m not sure how CUDA likes slices.

The CUDA version allocates a lot, even if the CPU version doesn’t. So probably it is copying. But anyway in my machine it is 150x faster than the CPU run.

I wonder if that can be improved by preallocating the space for the slices and copying.

[Previous page](https://discourse.julialang.org/t/julia-can-be-dramatically-slower-than-matlab-when-solving-2d-pde/122321.md?page=2)
