# Kron vs scalar product speed difference. python code faster?

**URL:** <https://discourse.julialang.org/t/kron-vs-scalar-product-speed-difference-python-code-faster/1460>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 13, 2017, 12:24pm UTC](https://discourse.julialang.org/t/kron-vs-scalar-product-speed-difference-python-code-faster/1460 "2017-01-13T12:24:27Z")\
**Posts on this page:** 2\
**Page:** 3

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 14, 2017, 10:15pm UTC](https://discourse.julialang.org/t/kron-vs-scalar-product-speed-difference-python-code-faster/1460/41 "2017-01-14T22:15:10Z")

</div>

> [@Evizero](#):
>
> So really there are a few non-obvious sources of performance penalties.

In v0.6 the vector form for `*` is deprecated so it will be more obvious though. (I think that v0.6 is a big enough change in this problem domain it is close enough to release that everything should start happening in its language)

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [January 14, 2017, 10:15pm UTC](https://discourse.julialang.org/t/kron-vs-scalar-product-speed-difference-python-code-faster/1460/42 "2017-01-14T22:15:25Z")

</div>

> [@Evizero](#):
>
> The \* (without the dot) causes broadcast fusion to stop

You can look at what is happening using `expand`.

In your version there isn’t any syntax-level broadcast fusion happening, which means that every operator basically creates a new temporary array

```julia
julia> expand(:(Delta_W .+= lr * ( x * ehp' - xneg * ehn')'))
:((Base.broadcast!)(+,Delta_W,Delta_W,A_mul_Bc(lr,A_mul_Bc(x,ehp) - A_mul_Bc(xneg,ehn))))

```

In the optimized version there are only broadcasted operators used. These dots `.` serve as a syntax sugar for broadcast and in master there is something happening that is called broadcast fusion. basically it merges all the dotted operators/function in one inner function that is broadcasted over the individual arrays. Thus no temporary memory for the inbetween computation needs to be allocated. Take a look:

```julia
julia> expand(:(Delta_W .+= lr .* (ehp .* x' .- ehn .* xneg')))
:($(Expr(:thunk, CodeInfo(:(begin 
        $(Expr(:thunk, CodeInfo(:(begin 
        global ##3#4
        const ##3#4
        $(Expr(:composite_type, Symbol("##3#4"), :((Core.svec)()), :((Core.svec)()), :(Core.Function), :((Core.svec)()), false, 0))
        return
    end))))
        $(Expr(:method, false, :((Core.svec)((Core.svec)(##3#4,Any,Any,Any,Any,Any,Any),(Core.svec)())), CodeInfo(:(begin 
        #temp#@_9 = #temp#@_4 * #temp#@_5
        #temp#@_8 = #temp#@_6 * #temp#@_7
        #temp#@_10 = #temp#@_9 - #temp#@_8
        #temp#@_11 = #temp#@_3 * #temp#@_10
        return #temp#@_2 + #temp#@_11
    end)), false))
        #3 = $(Expr(:new, Symbol("##3#4")))
        SSAValue(0) = #3
        SSAValue(1) = ctranspose(x)
        SSAValue(2) = ctranspose(xneg)
        return (Base.broadcast!)(SSAValue(0),Delta_W,Delta_W,lr,ehp,SSAValue(1),ehn,SSAValue(2))
    end)))))

```

EDIT: don’t be fooled by the syntax highlighting. there are no comments here. Somehow I am unable to turn of syntax highlighting.

EDIT: Notice the lower two lines that say `ctranspose` here the new rowvector addition comes into play that was mentioned before

[Previous page](https://discourse.julialang.org/t/kron-vs-scalar-product-speed-difference-python-code-faster/1460.md?page=2)
