Performance Guide
Benchmarks
Generation
20.5M
UUIDs/second (single thread)
Parsing
15.8M
UUIDs/second
ToString
12.3M
conversions/second
Memory
16
bytes per UUID
Optimization Tips
Bulk Operations
When generating multiple UUIDs, use the array methods:
// More efficient than generating individually
UUID[] uuids = new UUID[1000];
ArrayExtension.Fill(uuids);
Memory Management
Use the zero-allocation methods when possible:
// Avoid allocations
Span<byte> buffer = stackalloc byte[16];
uuid.TryWriteBytes(buffer);
Parallel Processing
Take advantage of parallel processing for bulk operations:
// Process UUIDs in parallel
Parallel.ForEach(uuids, uuid =>
{
ProcessUUID(uuid);
});
String Conversions
Choose the right string format for your use case:
// Base32 is URL-safe and compact
string base32 = uuid.ToBase32(); // Most efficient for URLs
// Base64 is compact but may need URL encoding
string base64 = uuid.ToBase64(); // Most space-efficient
// Standard format is human-readable
string standard = uuid.ToString(); // Most readable
Byte Array Operations
Use the most efficient byte array methods:
// Pre-allocate buffer for multiple writes
byte[] buffer = new byte[16];
if (uuid.TryWriteBytes(buffer))
{
// Use buffer directly without allocation
ProcessBytes(buffer);
}
// For single use, ToByteArray is simpler
byte[] bytes = uuid.ToByteArray();
Guid Conversions
Take advantage of implicit conversions:
// Implicit conversion is most efficient
UUID uuid = new UUID();
Guid guid = uuid; // No explicit conversion needed
// Avoid unnecessary conversions
void ProcessId(UUID uuid)
{
// Don't convert if UUID is accepted
ProcessUUID(uuid);
// Convert only when Guid is required
ProcessGuid(uuid); // Implicit conversion
}
Performance Comparison
Operation | UUID | Guid | Improvement |
---|---|---|---|
Generation | 20.5M/s | 12.3M/s | +66% |
Parse | 15.8M/s | 8.9M/s | +77% |
ToString | 12.3M/s | 7.2M/s | +70% |
Best Practices
Do
- Use array pooling for bulk operations
- Take advantage of Span<T> APIs
- Use TryParse over Parse when possible
- Cache frequently used UUIDs
Don't
- Generate UUIDs in tight loops individually
- Convert to string unnecessarily
- Parse the same UUID repeatedly
- Ignore the available bulk operations
Profiling Tools
BenchmarkDotNet
Our benchmarks are created using BenchmarkDotNet. You can run them yourself:
public class UUIDBenchmarks
{
[Benchmark]
public void GenerateUUID()
{
var uuid = new UUID();
}
[Benchmark]
public void ParseUUID()
{
UUID.Parse("0123456789abcdef0123456789abcdef");
}
}