Closed GoogleCodeExporter closed 9 years ago
Taking a second look at the list comprehension....
tmp = np.array([z[idsi[rid[0:wc[i]]]] for rid in rids])
which was taking ~0.37seconds.
This can be rewritten as,
tmp = z[idsi[rids[:,0:wc[i]]]]
Avoiding the for loop and the back and forth from numpy arrays to python lists
to numpy arrays, and takes ~0.008 seconds
The improved profiling results are,
182685 function calls (179077 primitive calls) in 0.904 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
9999 0.263 0.000 0.263 0.000 {method 'permutation' of 'mtrand.RandomState' objects}
12 0.047 0.004 0.047 0.004 {sum}
1 0.031 0.031 0.307 0.307 moran.py:555(__crand)
A ~79% time decrease over the original.
Committed in r1076
Original comment by schmi...@gmail.com
on 16 Jan 2012 at 6:45
Looking at the calls to numpy.random.permutation...
ncalls tottime percall cumtime percall filename:lineno(function)
9999 0.263 0.000 0.263 0.000 {method 'permutation' of 'mtrand.RandomState' objects}
Lines...
573 k = self.w.max_neighbors + 1
574 nn = range(self.n - 1)
575 rids = np.array([np.random.permutation(nn)[0:k] for i in prange])
The documentation for np.random.permuation states,
"""
If `x` is an integer, randomly permute ``np.arange(x)``.
If `x` is an array, make a copy and shuffle the elements
"""
Since we are passing in nn which is a python list, numpy must first
convert/copy it into a numpy array.
If we pass in a numpy array to begin with we get an immediate speed up since
the datastructure doesn't need to be converted from python's to numpy's
574 nn = np.arange(self.n - 1)
ncalls tottime percall cumtime percall filename:lineno(function)
9999 0.105 0.000 0.105 0.000 {method 'permutation' of 'mtrand.RandomState' objects}
We can get a very slight speed up from there by passing only the int inside of
the array...
574 nn = self.n - 1
ncalls tottime percall cumtime percall filename:lineno(function)
9999 0.098 0.000 0.098 0.000 {method 'permutation' of 'mtrand.RandomState' objects}
The improved profiling results are,
182684 function calls (179076 primitive calls) in 0.741 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
9999 0.098 0.000 0.098 0.000 {method 'permutation' of 'mtrand.RandomState' objects}
12 0.047 0.004 0.047 0.004 {sum}
1 0.031 0.031 0.141 0.141 moran.py:555(__crand)
...
~83% time decrease over the original.
Committed in r1078
Original comment by schmi...@gmail.com
on 16 Jan 2012 at 9:00
Original issue reported on code.google.com by
schmi...@gmail.com
on 16 Jan 2012 at 5:33Attachments: