Created
April 21, 2013 21:01
-
-
Save bwesterb/5431057 to your computer and use it in GitHub Desktop.
Faster random.shuffle for PyCrypto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
import timeit | |
import functools | |
import Crypto.Random.random | |
def shuffle(s): | |
r = Crypto.Random.random.randrange(0, math.factorial(len(s))) | |
for i in xrange(len(s)-1, 0, -1): | |
r, j = divmod(r, i+1) | |
s[i], s[j] = s[j], s[i] | |
print 'Python random.shuffle' | |
print timeit.repeat(functools.partial(random.shuffle, | |
range(100)), number=50, repeat=3) | |
print 'PyCrypto random.shuffle' | |
print timeit.repeat(functools.partial(Crypto.Random.random.shuffle, | |
range(100)), number=50, repeat=3) | |
print 'Suggested new PyCrypto random.shuffle' | |
print timeit.repeat(functools.partial(shuffle, | |
range(100)), number=50, repeat=3) |
Author
bwesterb
commented
Apr 21, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment