If I have a command y = A*B*x
where A
& B
are large matrices and x
& y
are vectors, will Julia preform y = ((A*B)*x)
or y = (A*(B*x))
?
The second option should be the best as it only has to allocate an extra vector rather than a large matrix.
The best way to verify this kind of thing is to dump the lowered code via @code_lowered
macro:
julia> @code_lowered A * B * x
CodeInfo(:(begin
nothing
return (Core._apply)(Base.afoldl, (Core.tuple)(Base.*, (a * b) * c), xs)
end))
Like many other languages, Julia does y = (A*B)*x
instead of y = A*(B*x)
, so it's up to you to explicitly use parens to reduce the allocation.
julia> using BenchmarkTools
julia> @btime $A * ($B * $x);
6.800 μs (2 allocations: 1.75 KiB)
julia> @btime $A * $B * $x;
45.453 μs (3 allocations: 79.08 KiB)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With