So why is the NumPy transpose .T
faster than np.transpose()
?
b = np.arange(10)
#Transpose .T
t=b.reshape(2,5).T
#Transpose function
t = np.transpose(b.reshape(2,5))
#Transpose function without wrapper
t = b.reshape(2,5).transpose()
I did a timeit
of both in Jupyter:
%timeit -n 1000 b.reshape(2,5).T
1000 loops, best of 3: 391 ns per loop
%timeit -n 1000 np.transpose(b.reshape(2,5))
1000 loops, best of 3: 600 ns per loop
%timeit -n 1000 b.reshape(2,5).transpose()
1000 loops, best of 3: 422 ns per loop
and to check scaleablility I did a larger matrix:
b = np.arange( 100000000)
%timeit -n 1000 b.reshape(10000,10000).T
1000 loops, best of 3: 390 ns per loop
%timeit -n 1000 np.transpose(b.reshape(10000,10000))
1000 loops, best of 3: 611 ns per loop
%timeit -n 1000 b.reshape(10000,10000).transpose()
1000 loops, best of 3: 435 ns per loop
In both cases the .T
method about 2x faster than the wrapper and a bit faster than using .transpose()
why is this? Is there a use case where np.transpose
would be better?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…