summaryrefslogtreecommitdiff
path: root/numpy/random
diff options
context:
space:
mode:
authorKevin Sheppard <kevin.k.sheppard@gmail.com>2018-05-06 23:21:09 +0100
committerKevin Sheppard <kevin.k.sheppard@gmail.com>2018-05-09 23:00:30 +0100
commit324b6df63d4b81cc430299c0ade5a750f1bc9f5f (patch)
tree191dbe119994c691fd0b6b94b99be03b365ba0d9 /numpy/random
parent0214bdb822ce2496b92fdd18f0a1ce4e5ab5aeef (diff)
downloadnumpy-324b6df63d4b81cc430299c0ade5a750f1bc9f5f.tar.gz
PERF: Improve performance of random permutation
Use fancy indexing instead of a shuffle to avoid unnecessary object access closes #11013
Diffstat (limited to 'numpy/random')
-rw-r--r--numpy/random/mtrand/mtrand.pyx22
1 files changed, 18 insertions, 4 deletions
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx
index 8ef153c15..b45b3146f 100644
--- a/numpy/random/mtrand/mtrand.pyx
+++ b/numpy/random/mtrand/mtrand.pyx
@@ -4901,10 +4901,24 @@ cdef class RandomState:
"""
if isinstance(x, (int, long, np.integer)):
arr = np.arange(x)
- else:
- arr = np.array(x)
- self.shuffle(arr)
- return arr
+ self.shuffle(arr)
+ return arr
+
+ arr = np.asarray(x)
+
+ # shuffle has fast-path for 1-d
+ if arr.ndim == 1:
+ # must return a copy
+ if arr is x:
+ arr = np.array(arr)
+ self.shuffle(arr)
+ return arr
+
+ # Shuffle index array, dtype to ensure fast path
+ idx = np.arange(arr.shape[0], dtype=np.intp)
+ self.shuffle(idx)
+ return arr[idx]
+
_rand = RandomState()
seed = _rand.seed