From 17bde11e622e3b7c896bacf85b8808ea9442c169 Mon Sep 17 00:00:00 2001 From: Dmitry Orlov Date: Thu, 21 Oct 2021 17:44:59 +0300 Subject: [PATCH] Generating uuid.uuid4() up to three times faster UUID uses numbers in the internal representation, this simple fix speeds up to three times the generation of uuid.uuid4() --- Lib/uuid.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 7aa01bb5c3..c9262c1ef9 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -46,6 +46,7 @@ Typical usage: import os import platform +import random import sys from enum import Enum @@ -777,7 +778,7 @@ def uuid3(namespace, name): def uuid4(): """Generate a random UUID.""" - return UUID(bytes=os.urandom(16), version=4) + return UUID(int=random.getrandbits(128), version=4) def uuid5(namespace, name): """Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" -- 2.30.1 (Apple Git-130)