[SciPy-user] exp2 or 2**

Yosef Meller yosefmel@post.tau.ac...
Tue Feb 12 02:08:58 CST 2008


On Thursday 31 January 2008 05:08:02 Tom Johnson wrote:
> Are there reasons to use scipy.special.exp2 over 2** when operating on
> arrays?  If so, what are they?  Using 2** seems to be faster...

I've run a test with 3 methods of finding exp2:

>>> from timeit import Timer
>>> Timer(setup="import scipy; a = scipy.arange(1000)", 
stmt="2**a").timeit(1000)
0.14900803565979004
>>> Timer(setup="import scipy; a = scipy.arange(1000)", 
stmt="scipy.special.exp2(a)").timeit(1000)
0.10561108589172363

>>> Timer(setup="import scipy; a = scipy.arange(64)", stmt="scipy.special.exp2
(a)").timeit(10000)
0.13341283798217773
>>> Timer(setup="import scipy; a = scipy.arange(64)", stmt="scipy.special.exp2
(a)").timeit(10000)
0.13643097877502441
>>> Timer(setup="import scipy; a = scipy.arange(64)", 
stmt="2**a").timeit(10000)
0.12771892547607422
>>> Timer(setup="import scipy; a = scipy.arange(64)", 
stmt="2**a").timeit(10000)
0.13031315803527832
>>> Timer(setup="import scipy; a = scipy.arange(64)", stmt="1 << 
a").timeit(10000)
0.048883914947509766
>>> Timer(setup="import scipy; a = scipy.arange(64)", stmt="1 << 
a").timeit(10000)
0.049239873886108398

So using exp2() is faster on my system for longer arrays (or bigger exponents) 
but slower for short ones. using shift left is always faster, but is only 
suitable for integers.

This is the source of exp2() with documentation on what's going on:
http://svn.scipy.org/svn/scipy/trunk/scipy/special/cephes/exp2.c

It seems that exp2() uses some polynomial approximation method instead of 
calculating the actual exponent.


More information about the SciPy-user mailing list