In [3]: f1 = rand(100000)
In [5]: f2 = rand(100000)
# Obvious method:
In [12]: timeit fmin = np.amin((f1, f2), axis=0); fmax = np.amax((f1, f2), axis=0)
10 loops, best of 3: 59.2 ms per loop
In [13]: timeit fmin, fmax = np.sort((f1, f2), axis=0)
10 loops, best of 3: 30.8 ms per loop
In [14]: timeit fmin = np.where(f2 < f1, f2, f1); fmax = np.where(f2 < f1, f1, f2)
100 loops, best of 3: 5.73 ms per loop
In [36]: f1 = rand(1000,100,100)
In [37]: f2 = rand(1000,100,100)
In [39]: timeit fmin = np.amin((f1, f2), axis=0); fmax = np.amax((f1, f2), axis=0)
1 loops, best of 3: 6.13 s per loop
In [40]: timeit fmin, fmax = np.sort((f1, f2), axis=0)
1 loops, best of 3: 3.3 s per loop
In [41]: timeit fmin = np.where(f2 < f1, f2, f1); fmax = np.where(f2 < f1, f1, f2)
1 loops, best of 3: 617 ms per loop
比如,也许有一种方法可以在两个where
命令中使用2个返回一次完成吗?
如果amin
比where
快得多,为什么它没有采用与where
相同的实现方式?
amin
和amax
的同义词,因为某种原因文档甚至没有相互链接。唉,确实快多了。 - endolith