julia> function my_multi_broadcast2(a)
@. 10 * (2*a^2 + 4*a^3) + 2 / a
end
my_multi_broadcast2 (generic function with 1 method)
不同之处在于
10 * (2*a.^2 + 4*a.^3) + 2 ./ a
*
二
+
没有广播。
书写
@. 10 * (2*a^2 + 4*a^3) + 2 / a
10 .* (2 .* a.^2 .+ 4 .* a.^3) .+ 2 ./ a
.
这里是性能的比较
julia> @btime my_multi_broadcast($arr);
58.146 ms (18 allocations: 61.04 MiB)
julia> @btime my_multi_broadcast2($arr);
5.982 ms (4 allocations: 7.63 MiB)
它与Pythran /C++相比如何,因为我们得到大约10x的加速?
arr
以书面形式落实:
julia> function my_multi_broadcast3(a)
@. a = 10 * (2*a^2 + 4*a^3) + 2 / a
end
my_multi_broadcast3 (generic function with 1 method)
julia> @btime my_multi_broadcast3($arr);
1.840 ms (0 allocations: 0 bytes)
这是更快,没有零分配(我不知道你是否想修改
啊
放置或创建一个新数组,以便我展示这两种方法)。