From f03c93097d159769a5e827c18514429c619d9620 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index f179d68e8265ac..087e5a0ce28b07 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -45,6 +45,8 @@ """ import os +import platform +import random import sys from enum import Enum, _simple_enum @@ -713,7 +715,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."""