# Can the compiler automatically optimize this code?

**URL:** <https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924>\
**Category:** General Usage\
**Created:** [December 22, 2018, 3:57pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924 "2018-12-22T15:57:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![song\_tebo](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@song\_tebo](https://discourse.julialang.org/u/song_tebo)\
**Post date:** [December 22, 2018, 3:57pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/1 "2018-12-22T15:57:28Z")

</div>

The code below is very slow:

```julia
w=rand(300,300);x=rand(300,300);
r=vec(rand(300,1));b=rand(300,1);
j=12;N=300;

function orig()    
    sAll=0.0;k=1;
    while k<=1000000
        sAll=sAll+sum(w[:,j].*(r + b[j]*x[:,j]));
        k=k+1;
    end
    return sAll;
end

```

And I write a helper function to speed it up:

```julia
function w_r_b_x_n(w,r,b,x,j,N)
    sAll=0.0;k=1;
    while k<=1000000
        s=0.0;i=1;bj=b[j];
        while i<=N
            @inbounds s+=(w[i,j]*(r[i] + bj*x[i,j])); 
            i=i+1;
        end
        k=k+1;sAll=sAll+s;
    end
    return sAll;
end

```

I wrote a bunch of similar magical helper functions. Can the compiler automatically optimize this code?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 22, 2018, 4:12pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/2 "2018-12-22T16:12:12Z")

</div>

Without information about the variables you’re using, it’s impossible to know. Can you produce a reproducible example?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 22, 2018, 4:14pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/3 "2018-12-22T16:14:00Z")

</div>

Please take a look at [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) otherwise it will be very hard to provide meaningful help.

---

<div class="post-metadata">

**Author:** ![song\_tebo](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@song\_tebo](https://discourse.julialang.org/u/song_tebo)\
**Post date:** [December 22, 2018, 5:03pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/4 "2018-12-22T17:03:28Z")

</div>

Thanks a lot. This is the test code:

```julia
w=rand(300,300);x=rand(300,300);
r=vec(rand(300,1));b=rand(300,1);
j=12;N=300;

function orig()    
    sAll=0.0;k=1;
    while k<=1000000
        sAll=sAll+sum(w[:,j].*(r + b[j]*x[:,j]));
        k=k+1;
    end
    return sAll;
end

function w_r_b_x_n(w,r,b,x,j,N)
    sAll=0.0;k=1;
    while k<=1000000
        s=0.0;i=1;bj=b[j];
        while i<=N
            @inbounds s+=(w[i,j]*(r[i] + bj*x[i,j])); 
            i=i+1;
        end
        k=k+1;sAll=sAll+s;
    end
    return sAll;
end

@time orig()
@time w_r_b_x_n(w,r,b,x,j,N)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 22, 2018, 5:21pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/5 "2018-12-22T17:21:24Z")

</div>

Please add three backtics like this ``` before and after your code.

You needn’t write all your code in loops to achieve performance. You can use the `sum` intrinsic which is very fast and more accurate, just take care of unnecessary allocations. Write the sum like this:

```julia
sum( @. @views w[:,j] * (r + b[j]*d[:,j]) )

```

and it will be almost as fast as the loop.

Curerntly, `@views` still allocates, but this may change in the future and you will not need to write loops for performance.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 22, 2018, 6:50pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/6 "2018-12-22T18:50:49Z")

</div>

You can also do ` sum(w[i, j] * (r + b[j] * x[i, j]) for i in 1:N)` to avoid all allocations.

(BTW, instead of `while` and the manual handling of `i`, better do `for i in 1:N`.)

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 22, 2018, 8:34pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/7 "2018-12-22T20:34:49Z")

</div>

If I get it correctly, I think it will not give the exact same result as sum(array), [see this for example](https://discourse.julialang.org/t/imprecision-of-mean-over-iterators-of-large-vectors/18759).

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 22, 2018, 9:36pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/8 "2018-12-22T21:36:29Z")

</div>

Indeed currently it won’t, but depending on the situation it may or may not matter.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 26, 2018, 4:16pm UTC](https://discourse.julialang.org/t/can-the-compiler-automatically-optimize-this-code/18924/9 "2018-12-26T16:16:07Z")

</div>

Seems like making all your globals const would fix performance more easily.
