访问矩阵时SPLAT操作员的性能
假设我们有一个矩阵和要访问该矩阵的指数的矩阵和向量:
matrix = Matrix{Float64}(undef, 5000, 4000)
point = [1244, 3353]
我们可以使用Splat Operator(...)扩展点向量以访问矩阵,或者我们可以明确使用点[1 ]和Point [2]作为指数:
matrix[point...]
vs
matrix[point[1], point[2]]
第一个更优雅,但显然也更慢:
@btime $matrix[$point...]
70.083 ns (4 allocations: 64 bytes)
@btime $matrix[$point[1], $point[2]]
1.800 ns (0 allocations: 0 bytes)
这种差异来自何处?有没有办法使SPLAT操作员更具性能,或者如果我关心性能,我应该简单地使用其他解决方案?
Let's say we have a matrix and a vector of the indices at which we want to access that matrix:
matrix = Matrix{Float64}(undef, 5000, 4000)
point = [1244, 3353]
We can use the splat operator (...) to expand the point vector to access the matrix, or we can explicitly use point[1] and point[2] as the indices:
matrix[point...]
vs
matrix[point[1], point[2]]
The first is more elegant, but apparently also much slower:
@btime $matrix[$point...]
70.083 ns (4 allocations: 64 bytes)
@btime $matrix[$point[1], $point[2]]
1.800 ns (0 allocations: 0 bytes)
Where does this difference come from? Is there a way to make the splat operator more performant or should I simply use the other solution if I care about performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
元组似乎与明确的破坏一样快:(
我认为后三个之间的区别大多是微不足道的 - 在较早的试验中,第二个“赢了”。
搭配 如果您直接插入元组,就不足为奇了。
Splatting a tuple seems to be as fast as explicit destruction:
(I presume the difference between the latter three are mostly insignificant -- in earlier trials, the third "won" over the second.)
Variadic function arguments are internally passed as tuples, so it's no surprise there is no overhead if you directly insert a tuple.