Benchmarks - Taiizor/UUID GitHub Wiki

📊 Benchmarks

Performance Results

All benchmarks were run on the following configuration:

  • CPU: Intel Core i9-13900K
  • RAM: 64GB DDR5-6000
  • OS: Windows 11 Pro
  • .NET SDK: 9.0.100

UUID Generation

Operations per second (higher is better):

Operation Single Thread 4 Threads 8 Threads 16 Threads
New 20.5M 78.2M 152.3M 289.8M
Parse 12.8M 48.9M 94.2M 180.1M
ToString 21.9M 84.3M 162.8M 310.5M

Comparison with Other Libraries

Operations per second (higher is better):

Operation UUID Guid UUID.Net NUlid
Generation 20.5M 15.2M 12.8M 14.1M
Parse 12.8M 8.9M 7.2M 8.1M
ToString 21.9M 16.4M 14.2M 15.8M
Comparison 793.7M 685.3M 612.8M 589.4M

Memory Usage

Bytes allocated per operation:

Operation Bytes
New 16
Parse 24
ToString 32
ToBase32 26
ToBase64 24
Compare 0

Running Benchmarks

Using BenchmarkDotNet

[MemoryDiagnoser]
public class UUIDBenchmarks
{
    private static readonly UUID _uuid = new();
    private static readonly string _uuidString = _uuid.ToString();
    
    [Benchmark]
    public UUID Generate() => new UUID();
    
    [Benchmark]
    public string ToString() => _uuid.ToString();
    
    [Benchmark]
    public UUID Parse() => UUID.Parse(_uuidString);
}

Command Line

dotnet run -c Release --project benchmark/UUID.Benchmarks

Custom Benchmarking

Generation Performance

public static class CustomBenchmark
{
    public static void MeasureGeneration(int count)
    {
        var sw = Stopwatch.StartNew();
        for (int i = 0; i < count; i++)
        {
            _ = new UUID();
        }
        sw.Stop();
        
        Console.WriteLine($"Generated {count:N0} UUIDs in {sw.ElapsedMilliseconds}ms");
        Console.WriteLine($"Rate: {count / (sw.ElapsedMilliseconds / 1000.0):N0} UUIDs/second");
    }
}

Parallel Performance

public static void MeasureParallelGeneration(int count, int threads)
{
    var sw = Stopwatch.StartNew();
    
    Parallel.For(0, threads, threadId =>
    {
        int perThread = count / threads;
        for (int i = 0; i < perThread; i++)
        {
            _ = new UUID();
        }
    });
    
    sw.Stop();
    Console.WriteLine($"Generated {count:N0} UUIDs using {threads} threads");
    Console.WriteLine($"Total time: {sw.ElapsedMilliseconds}ms");
    Console.WriteLine($"Rate: {count / (sw.ElapsedMilliseconds / 1000.0):N0} UUIDs/second");
}

Memory Profiling

Using Memory Diagnoser

[MemoryDiagnoser]
public class UUIDMemoryBenchmarks
{
    private static readonly UUID _uuid = new();
    private static readonly byte[] _buffer = new byte[16];
    private static readonly char[] _charBuffer = new char[32];
    
    [Benchmark]
    public void ToStringWithBuffer() => _uuid.TryWriteStringify(_charBuffer);
    
    [Benchmark]
    public void ToBytesWithBuffer() => _uuid.TryWriteBytes(_buffer);
}

Optimization Tips

  1. Reuse Buffers
// Efficient string conversion
private readonly char[] _buffer = new char[32];
public string FastToString(UUID uuid)
{
    return uuid.TryWriteStringify(_buffer) 
        ? new string(_buffer) 
        : uuid.ToString();
}
  1. Bulk Operations
// Efficient bulk generation
public UUID[] GenerateBulk(int count)
{
    var result = new UUID[count];
    Parallel.For(0, count, i => result[i] = new UUID());
    return result;
}
  1. Thread-Local Storage
private static readonly ThreadLocal<char[]> BufferPool =
    new(() => new char[32]);

public string ThreadSafeToString(UUID uuid)
{
    var buffer = BufferPool.Value;
    return uuid.TryWriteStringify(buffer)
        ? new string(buffer)
        : uuid.ToString();
}

Additional Resources

⚠️ **GitHub.com Fallback** ⚠️