Issue
I'm trying to find the fastest way to generate a shuffled array of indices. A shuffled range from 0 to a number, so to say.
Currently I've compared these two methods:
setup = "import numpy as np; idxlen = 1000; rng = np.random.default_rng(); matrix = -np.ones([100,idxlen],dtype=int)"
test1 = "rand_idxs = np.arange(idxlen);
rng.shuffle(rand_idxs);
matrix[0][:] = rand_idxs"
test2 = "matrix[0][:] = rng.choice(range(idxlen),idxlen,replace=False)"
print(timeit.timeit(test1, setup, number = 10000))
print(timeit.timeit(test2, setup, number = 10000))
Returning the following timings:
0.1846354000736028
0.950088300043717
I assume that under the hood, the rng.shuffle() method randomizes the indices of the array past to it, and rearranges it according to those indices. Now, since I'm generating an array containing every integer from 0 to a certain number, this practically corresponds to generating randomized indices. So I can't shake the feeling that there is a faster way to do what I'm trying to achieve. Is there?
Solution
If you remove range from the test two then you will see changes and find test2
is faster then first
import timeit
setup = "import numpy as np; idxlen = 1000; rng = np.random.default_rng(); matrix = -np.ones([100,idxlen],dtype=int)"
test1 = "rand_idxs = np.arange(idxlen); rng.shuffle(rand_idxs); matrix[0][:] = rand_idxs"
test2 = "matrix[0][:] = rng.choice(idxlen,idxlen,replace=False)"
print(timeit.timeit(test1, setup, number = 10000))
print(timeit.timeit(test2, setup, number = 10000))
Now for testing
for idxlen=1000
0.15045420000024023
0.24143790000016452
for idxlen=10000
1.4334205999984988
1.3395810999973037
for idxlen=100000
14.489788099999714
8.599601199999597
Also there is one more method that is np.random.Generator.permutation
And the testing for this method are these
test3 = "matrix[0][:] = rng.permutation(idxlen)"
for idxlen=1000
0.1567758999990474
for idxlen=10000
1.4174363000020094
for idxlen=100000
13.964398899999651
In the end the comparison of timing between all three
Graph for number=10000
Answered By - codester_09
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.