我是 julia 的新用户,我正在尝试了解在 julia 中编写快速代码的最佳实践是什么。我主要是在数组/矩阵中进行元素明智的操作。我尝试了一些代码来检查哪一个可以让我获得更高的速度
fbroadcast(a,b) = a .*= b;
function fcicle(a,b)
@inbounds @simd for i in eachindex(a)
a[i] *= b[i];
end
end
a = rand(100,100);
b = rand(100,100);
@btime fbroadcast(a,b)
@btime fcicle(a,b)
Run Code Online (Sandbox Code Playgroud)
使用 for 的功能实现了广播版本的 2 倍左右的速度。两种情况有什么区别?我希望广播在内部循环操作,这与我在 fcicle 上所做的非常相似。最后,有没有什么方法可以通过像 a .*= b 这样的简短语法实现最佳速度?
非常感谢,迪伦