I am trying to define a function that contains an inner loop for simulating an integral.
The problem is speed. Evaluating the function once can take up to 30 seconds on
Splitting data only, after your comment on 28 Nov:
import sys
import time
import numpy as np
def splitdata( data, n, start=1971 ):
""" split data into n pieces, where col 1 == start .. start + n """
# not fancy, fast enough for small n
split = n * [None]
for j in range(n):
split[j] = data[ data[:,1] == start + j ]
return split # [ arrays: col1 0, col1 1 ... ]
#...........................................................................
N = 2237
ncol = 21
start = 1971
n = 20
seed = 1
exec "\n".join( sys.argv[1:] ) # run this.py N= ...
np.set_printoptions( 2, threshold=100, suppress=True ) # .2f
np.random.seed(seed)
print "N=%d ncol=%d n=%d" % (N, ncol, n)
data = np.random.uniform( start, start + n, (N,ncol) )
data[:,1] = data[:,1].round()
t0 = time.time()
split = splitdata( data, n, start ) # n pieces
print "time: %.0f us splitdata" % ((time.time() - t0) * 1e6)
for y, yeardata in enumerate(split):
print "%d %d %g" % (start + y, len(yeardata), yeardata[:,0].sum())
-->
time: 27632 us splitdata # old mac ppc
1971 69 136638
1972 138 273292
...