问题描述
信不信由你,在分析当前代码后,numpy数组还原的重复操作占用了很大一部分运行时间.我现在拥有的是基于视图的常见方法:
Believe it or not, after profiling my current code, the repetitive operation of numpy array reversion ate a giant chunk of the running time. What I have right now is the common view-based method:
reversed_arr = arr[::-1]
还有其他方法可以更有效地执行此操作吗,或者仅仅是我对不切实际的numpy性能的痴迷而产生的错觉?
Is there any other way to do it more efficiently, or is it just an illusion from my obsession with unrealistic numpy performance?
推荐答案
创建reversed_arr
时,您将在原始数组中创建视图.然后,您可以更改原始数组,视图将更新以反映所做的更改.
When you create reversed_arr
you are creating a view into the original array. You can then change the original array, and the view will update to reflect the changes.
您重新创建视图的次数是否比您需要的次数更多?您应该可以执行以下操作:
Are you re-creating the view more often than you need to? You should be able to do something like this:
arr = np.array(some_sequence)
reversed_arr = arr[::-1]
do_something(arr)
look_at(reversed_arr)
do_something_else(arr)
look_at(reversed_arr)
我不是numpy专家,但这似乎是用numpy做事的最快方法.如果这是您已经在做的,我认为您无法对此进行改进.
I'm not a numpy expert, but this seems like it would be the fastest way to do things in numpy. If this is what you are already doing, I don't think you can improve on it.
P.S.在这里对numpy视图进行了很好的讨论:
P.S. Great discussion of numpy views here:
这篇关于反转numpy数组的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!