Does new "dot" syntax fuse?

183 views
Skip to first unread message

Alexey Cherkaev

unread,
Oct 20, 2016, 4:55:25 PM10/20/16
to julia-users

Hi all!

Consider example:

# collect to produce vectors - not strictly necessary though
y = collect(linspace(0.0, 1.0, 1000))
z = collect(linspace(0.5,3.0, 1000))

x = zeros(Float64, 1000)

If I do

@time x .= sin.(y)

The timing output is

  0.000057 seconds (7 allocations: 208 bytes)

So, OK, no, let’s call it, “real” allocation. However, if I do:

@time x .= cos.(sin.(y))

I get

  0.018246 seconds (7.16 k allocations: 322.747 KB)

Or

@time x .= sin.(y) .+ cos.(z)
  0.000376 seconds (63 allocations: 25.984 KB)

Better, but still 26 KB allocated! I was under impression that .-operations fuse, producing no intermediate arrays. Am I wrong?

Kristoffer Carlsson

unread,
Oct 20, 2016, 5:20:00 PM10/20/16
to julia-users
Firstly, don't benchmark in global scope, put things in functions. Secondly, see https://github.com/JuliaLang/julia/pull/17623 for .+ and company.

Steven G. Johnson

unread,
Oct 20, 2016, 9:59:59 PM10/20/16
to julia-users
Putting things into a function to avoid benchmarking in global scope:
 

foo!(x,y) = x .= cos.(sin.(y))

y = rand(1000); x = similar(y);

@time foo!(x, y);


gives (after running @time twice to eliminate the compilation time):


  0.000035 seconds (5 allocations: 176 bytes)


i.e. it is not allocating any arrays, and all the loops are fused.


Binary operations like .+ will not be fused until Julia 0.6, however.  (This is documented in the manual.)

Alexey Cherkaev

unread,
Oct 21, 2016, 3:51:27 AM10/21/16
to julia-users
Ay, how many times I've done it! Still forget about global variables!

OK, I see now about `.+` and friends.

Thanks!

Mauro

unread,
Oct 21, 2016, 5:23:07 AM10/21/16
to julia...@googlegroups.com
On Fri, 2016-10-21 at 09:51, Alexey Cherkaev <alexey....@gmail.com> wrote:
> Ay, how many times I've done it! Still forget about global variables!
>
> OK, I see now about `.+` and friends.

As a work-around until 0.6: If you use the functional form of binary
operators then they get fused too:

x .= (+).(y,z)
Reply all
Reply to author
Forward
0 new messages