# Question on Julia Blog Post regarding Performance

**URL:** https://discourse.julialang.org/t/question-on-julia-blog-post-regarding-performance/87342
**Category:** Performance
**Tags:** optimization, inbounds
**Created:** [September 16, 2022, 3:23am UTC](https://discourse.julialang.org/t/question-on-julia-blog-post-regarding-performance/87342 "2022-09-16T03:23:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)
#### Post date: [September 16, 2022, 3:23am UTC](https://discourse.julialang.org/t/question-on-julia-blog-post-regarding-performance/87342/1 "2022-09-16T03:23:24Z")

</div>

I was searching around for Julia comparisons to C++ and found [this](https://flow.byu.edu/posts/julia-c++) write-up from 2019.

I follow most of it but one point I was curious about is near the end where they implemented `@simd`, `@fastmath`, and `@inbounds`.

I would have expected to see `@inbounds` decorating the hot inner loop (I think that is line `for Pj in particles` unless I missed it). Instead they only have `@simd` decorating it and then wrap the actual loop calculations in a `begin` block which is decorated with `@fastmath @inbounds`.

It has never occurred to me to wrap such a large chunk of code in a `begin` block (I only find myself using them in pluto notebooks) so it might not be unusual at all but just new to me.

Why do that instead of putting it on the `for` loop? In fact, why not do `@simd @fastmath @inbounds for Pj in particles`? Is there a difference and if so what is it?

The entire code snippet in question, copy/pasted from the above link, is shown below for reference.

```julia
"""
    Implementation of @simd, @fastmath, and @inbounds
"""
function P2P_FINAL(particles, g_dgdr, U, J)

    for (i, Pi) in enumerate(particles)
        @simd for Pj in particles

            @fastmath @inbounds begin
                dX1 = Pi.X1 - Pj.X1
                dX2 = Pi.X2 - Pj.X2
                dX3 = Pi.X3 - Pj.X3
                r = sqrt(dX1*dX1 + dX2*dX2 + dX3*dX3)

                if r != 0

                    # g_σ and ∂gσ∂r
                    gsgm, dgsgmdr = g_dgdr(r / Pj.sigma)

                    # K × Γp
                    crss1 = -const4 / r^3 * ( dX2*Pj.Gamma3 - dX3*Pj.Gamma2 )
                    crss2 = -const4 / r^3 * ( dX3*Pj.Gamma1 - dX1*Pj.Gamma3 )
                    crss3 = -const4 / r^3 * ( dX1*Pj.Gamma2 - dX2*Pj.Gamma1 )

                    # U = ∑g_σ(x-xp) * K(x-xp) × Γp
                    U[1, i] += gsgm * crss1
                    U[2, i] += gsgm * crss2
                    U[3, i] += gsgm * crss3

                    # ∂u∂xj(x) = ∑[∂gσ∂xj(x−xp) * K(x−xp)×Γp + gσ(x−xp) * ∂K∂xj(x−xp)×Γp]
                    # ∂u∂xj(x) += ∑p[(Δxj∂gσ∂r/(σr) − 3Δxjgσ/r^2) K(Δx)×Γp
                    aux = dgsgmdr/(Pj.sigma*r)* - 3*gsgm /r^2
                    # j=1
                    J[1, 1, i] += aux * crss1 * dX1
                    J[2, 1, i] += aux * crss2 * dX1
                    J[3, 1, i] += aux * crss3 * dX1
                    # j=2
                    J[1, 2, i] += aux * crss1 * dX2
                    J[2, 2, i] += aux * crss2 * dX2
                    J[3, 2, i] += aux * crss3 * dX2
                    # j=3
                    J[1, 3, i] += aux * crss1 * dX3
                    J[2, 3, i] += aux * crss2 * dX3
                    J[3, 3, i] += aux * crss3 * dX3

                    # Adds the Kronecker delta term
                    aux = -const4 * gsgm / r^3
                    # j=1
                    J[2, 1, i] -= aux * Pj.Gamma3
                    J[3, 1, i] += aux * Pj.Gamma2
                    # j=2
                    J[1, 2, i] += aux * Pj.Gamma3
                    J[3, 2, i] -= aux * Pj.Gamma1
                    # j=3
                    J[1, 3, i] -= aux * Pj.Gamma2
                    J[2, 3, i] += aux * Pj.Gamma1

                end
            end

        end
    end

end

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [September 16, 2022, 3:48am UTC](https://discourse.julialang.org/t/question-on-julia-blog-post-regarding-performance/87342/2 "2022-09-16T03:48:15Z")

</div>

> [@TI36XPro](#):
>
> Why do that instead of putting it on the `for` loop? In fact, why not do `@simd @fastmath @inbounds for Pj in particles`? Is there a difference and if so what is it?

No, it wouldn’t make a difference. Also, I doubt that `@simd` annotation is doing anything here, due to the access pattern in `J`, and the branching on `r`.
