I am generating a large matrix (100x100, let's call it X) with random numbers, with numpy.matrix() so that I have a numpy.ndarray.
I have been wondering if there are any difference between the two operations:
- numpy.transpose(X)
- X.T
I have measured the time of each operation in a loop with a range of 1000 and it seems that X.T is significantly faster than numpy.transpose(X)
Added Benchmarks:
For a 100x100 matrix I got the following results with X.T and numpy.tranpose(X)
In a 10.000 range loop:
- 7421/10.000: X.T fastest
- 1256/10.000: numpy.transpose(X) fastest
- 1323/10.000: Same computation time or difference too small to determine
Added the code below
import numpy as np
import time
np_transpose_count = 0
T_transpose_count = 0
equal_count = 0
for i in range(10000):
Se = np.random.rand(100,100)
tic1 =time.clock()
ST_T = Se.T
toc1=time.clock()
tic2 =time.clock()
ST_np = np.transpose(Se)
toc2=time.clock()
if (toc1-tic1) < (toc2-tic2):
T_transpose_count+=1
elif (toc1-tic1) > (toc2-tic2):
np_transpose_count+=1
else:
equal_count+=1
print(T_transpose_count, np_transpose_count, equal_count)
Best regards Whir
np.transpose(X)
is 120-130% slower thanx.T
on my system. Maybe there's a small amount of extra overhead for the function call (x.transpose()
is a function whilex.T
is not).np.transpose()
is a python function with some wrapping code, so there's some extra overhead there. – Killiecrankie