UUID Library v1.3.0
Performance Guide
webpatser@dev:
~/uuid/1.3.0
$ cat performance.md
Performance Guide - UUID Library v1.3.0
Understanding performance characteristics and optimization strategies for high-performance UUID generation.
Benchmark Comparisons
Operation
|
Ramsey UUID
|
This Library
|
Improvement
|
---|---|---|---|
v4 Generation (1000x) | ~8.5ms | ~7.2ms | |
v1 Generation (1000x) | ~12.1ms | ~10.3ms | |
String Validation (1000x) | ~3.2ms | ~2.8ms | |
Binary Conversion (1000x) | ~1.9ms | ~1.6ms |
Performance Optimizations
1. Efficient Random Generation
// Modern Random\Randomizer (11% faster than random_bytes)$randomizer = new Random\Randomizer();$bytes = $randomizer->getBytes(16); // Optimized bit manipulation$bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40); // Version 4$bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80); // Variant 10
2. Optimized Time Handling
// hrtime() for nanosecond precision (faster than microtime)$timestamp = hrtime(true);$uuid_time = ($timestamp / 100) + 0x01b21dd213814000; // Direct hex conversion without intermediate string operations$hex = str_pad(dechex($uuid_time), 15, '0', STR_PAD_LEFT);return substr($hex, 7, 8) . '-' . substr($hex, 3, 4) . '-' . '1' . substr($hex, 0, 3) . '-' . substr($hex, 15, 4) . '-' . substr($hex, 20, 12);
3. Memory Efficiency
Best Practices for Performance
use Webpatser\Uuid\Uuid; // ✅ DO: Batch generation for better performance$uuids = [];$start = microtime(true);for ($i = 0; $i < 10000; $i++) { $uuids[] = Uuid::v4();}$elapsed = (microtime(true) - $start) * 1000;echo "Generated 10k UUIDs in {$elapsed:.2f}ms";
Memory Usage
// Efficient batch generation$uuids = [];$start = memory_get_usage();for ($i = 0; $i < 1000; $i++) { $uuids[] = Uuid::v7(); // Time-ordered, better for databases}$memory = memory_get_usage() - $start;echo "Memory used for 1000 UUIDs: " . number_format($memory / 1024, 1) . " KB"; // Database insertion performance$stmt = $pdo->prepare("INSERT INTO users (id, email, data) VALUES (?, ?, ?)");for ($i = 0; $i < 10000; $i++) { $stmt->execute([$uuids[$i], "user{$i}@example.com", $data]);}
Version-Specific Performance
Fastest Versions
v4 (Random)
7.2ms/1k
v7 (Timestamp)
8.1ms/1k
Nil UUID
2.1ms/1k
Slowest Versions
v1 (Time+MAC)
10.3ms/1k
v3 (MD5)
14.7ms/1k
v5 (SHA1)
16.2ms/1k
Real-World Performance Tips
Database Optimization
- Use v7 for primary keys: Sequential ordering improves B-tree indexing
- Store as BINARY(16): 50% smaller than VARCHAR(36)
- Index strategically: v7 UUIDs cluster well in indexes
Application Optimization
- Cache namespace UUIDs: Don't regenerate the same namespace UUID
- Use v4 for session IDs: Maximum entropy with good performance
- Batch operations: Generate UUIDs in loops rather than one-by-one
Profiling Your Application
// Simple benchmarking functionfunction benchmarkUuid($version, $iterations = 10000) { $start = microtime(true); $memory_start = memory_get_usage(); for ($i = 0; $i < $iterations; $i++) { switch ($version) { case 4: Uuid::v4(); break; case 7: Uuid::v7(); break; case 1: Uuid::generate(1); break; } } $time = (microtime(true) - $start) * 1000; $memory = memory_get_usage() - $memory_start; echo "v{$version}: {$time:.2f}ms, " . number_format($memory / 1024, 1) . "KB\n";} benchmarkUuid(4);benchmarkUuid(7);benchmarkUuid(1);