wordsforthewise wordsforthewise - 4 years ago 103
Python Question

Why is numpy.ndarray.T so much faster than numpy.transpose(numpy.ndarray)?

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download