c
! www.simula.no/˜hpl
Array computat ion s
Arithmetic operations can be used with arrays:
b=3
*
a-1 #aisarray,bbecomesarray
1) compute t1 = 3
*
a,2)computet2= t1 - 1,3)setb=t2
Array operations are much faster than element-wise operations:
>>> import time # module for measuring CPU time
>>> a = linspace(0, 1, 1E+07) # create some array
>>> t0 = time.clock()
>>> b = 3
*
a-1
>>> t1 = time.clock() # t1-t0 is the CPU time of 3
*
a-1
>>> for i in xrange(a.size): b[i] = 3
*
a[i] - 1
>>> t2 = time.clock()
>>> print ’3
*
a-1: %g sec, loop: %g sec’ % (t1-t0, t2-t1)
3
*
a-1: 2.09 sec, loop: 31.27 sec
Numerical Python – p. 254/728
Comentarios a estos manuales