Differences of transpose functions in python
Asked Answered
V

1

8

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:

  1. numpy.transpose(X)
  2. 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

Vlf answered 24/5, 2016 at 8:53 Comment(2)
Interesting. I don't understand the units of your benchmark. Could you update to say? On my system, looks like X.T is 20-30% faster than X.transpose() (using %timeit on 100x100 or 10000x10000 array).Killiecrankie
Also, np.transpose(X) is 120-130% slower than x.T on my system. Maybe there's a small amount of extra overhead for the function call (x.transpose() is a function while x.T is not). np.transpose() is a python function with some wrapping code, so there's some extra overhead there.Killiecrankie
B
9

Using Ipython %timeit magic I get:

In [218]: X=np.ones((100,100))

In [219]: timeit X.T
1000000 loops, best of 3: 379 ns per loop

In [220]: timeit X.transpose()
1000000 loops, best of 3: 470 ns per loop

In [221]: timeit np.transpose(X)
1000000 loops, best of 3: 993 ns per loop

In [222]: timeit X+1
10000 loops, best of 3: 21.6 µs per loop

So yes, .T is fastest, and the function slowest. But compare those times with the time for a simple addition

Or a copy or slice

In [223]: timeit X.copy()
100000 loops, best of 3: 10.8 µs per loop

In [224]: timeit X[:]
1000000 loops, best of 3: 465 ns per loop

Transpose in all its forms returns a new array object, with new shape and strides, but with a shared data buffer (Look at the .__array_interface__ dictionary to see that). So it takes about the same time as other actions that return a view. But none of the transpose functions makes a copy of the data or iterates through it. So the time differences are just the result of calling over head.

Again with ipython magic

np.transpose??
def transpose(a, axes=None):
    try:
        transpose = a.transpose
    except AttributeError:
        return _wrapit(a, 'transpose', axes)
    return transpose(axes)

So np.function(X) ends up calling X.transpose().

I'd have to look at the numpy code, but I recall that .T is implemented as an attribute (not quite the same as a property). I suspect it is faster because it doesn't use the axes parameter, and thus saves a C function call or two.

Baudin answered 24/5, 2016 at 16:3 Comment(1)
Thank you for your detailed reply. It was very useful for my understanding of this.Vlf

© 2022 - 2024 — McMap. All rights reserved.