This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mattchaput
Recipients mattchaput
Date 2011-09-15.16:30:16
SpamBayes Score 2.2162921e-10
Marked as misclassified No
Message-id <1316104217.68.0.381815581788.issue12986@psf.upfronthosting.co.za>
In-reply-to
Content
Currently the 'uuid' module uses os.urandom (in the absence of a system UUID generation function) to generate random UUIDs in the uuid.uudi4() function. This patch changes the implementation of uuid4() to use random.getrandbits() as the source of randomness instead, for the following reasons:

* In my quick tests, using getrandbits() is much faster on Windows and Linux. Some applications do need to generate UUIDs quickly.

  >>> setup = "import uuid, os, random"
  >>> ur = "uuid.UUID(bytes=os.urandom(16), version=4)"
  >>> grb = "uuid.UUID(int=random.getrandbits(128), version=4)"
  >>> # Windows --------
  >>> timeit.Timer(ur, setup).timeit()
  22.861042160383903
  >>> timeit.Timer(grb, setup).timeit()
  3.8689128309085135
  >>> # Linux --------
  >>> timeit.Timer(ur, setup).timeit()
  29.32686185836792
  >> timeit.Timer(grb, setup).timeit()
  3.7429409027099609

* The patched code is cleaner. It avoids the try...finally required by the possibly unavailable os.urandom function, and the fallback to generating random bytes.
History
Date User Action Args
2011-09-15 16:30:17mattchaputsetrecipients: + mattchaput
2011-09-15 16:30:17mattchaputsetmessageid: <1316104217.68.0.381815581788.issue12986@psf.upfronthosting.co.za>
2011-09-15 16:30:16mattchaputlinkissue12986 messages
2011-09-15 16:30:16mattchaputcreate