7

Recently I came across someone using numpy.transpose instead of numpy.ndarray.T. I was curious so I timed it:

from timeit import timeit
import numpy as np

array1015 = np.random.rand(10,15)

def nptrans():
    np.transpose(array1015)

def npt():
    array1015.T

print(timeit(nptrans))
print(timeit(npt))

The results were:

np.transpose: 1.25864219666

np.ndarray.T: 0.720939874649

Why? Shouldn't they be doing the same thing under the hood? Maybe np.transpose is doing some sort of error checking or something that slows it down?

0

1 Answer 1

7

First, the operations are so fast it doesn't really matter if one optimizes there!

%timeit nptrans()  # 100000 loops, best of 3: 2.11 µs per loop
%timeit npt()      # 1000000 loops, best of 3: 905 ns per loop

Optimizing this doesn't make sense except you would be doing millions of transposes and nothing else. Even adding them is much, much slower:

%timeit array1015 + array1015  # 100000 loops, best of 3: 3.55 µs per loop

and addition should be really, really fast!

However there is some overhead in np.transpose that isn't present in np.ndarray.T:

  • np.transpose in the end calls the objects .transpose-method which means it has to look-up the method of the object and call it.
  • To avoid repeating their code the developers packed the function that actually calls the method in a seperate function. => One more function call.

So the overhead you see is the result of 2 function calls and a getattr-call. The np.transpose function is actually python so you can see the overhead quite easily (I removed comments):

def transpose(a, axes=None):
    return _wrapfunc(a, 'transpose', axes)  # extra function call

def _wrapfunc(obj, method, *args, **kwds):
    try:
        return getattr(obj, method)(*args, **kwds)  # here it finally calls ndarray.transpose()
    except (AttributeError, TypeError):
        return _wrapit(obj, method, *args, **kwds)
Sign up to request clarification or add additional context in comments.

1 Comment

I see. Well the code in question was a neural network for a Udacity project, so it might make some difference, I'll have to check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.