-
-
Notifications
You must be signed in to change notification settings - Fork 3
Benchmarks
Taiizor edited this page Dec 18, 2024
·
5 revisions
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
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 |
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 |
Bytes allocated per operation:
| Operation | Bytes |
|---|---|
| New | 16 |
| Parse | 24 |
| ToString | 32 |
| ToBase32 | 26 |
| ToBase64 | 24 |
| Compare | 0 |
[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);
}dotnet run -c Release --project benchmark/UUID.Benchmarkspublic 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");
}
}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");
}[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);
}- 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();
}- 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;
}- 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();
}- BenchmarkDotNet documentation
- .NET Performance Best Practices
- Check our FAQ section
- Visit Debugging and Diagnostics
- Review Performance guidelines
- Join our Discord community
- Ask on Stack Overflow
- Report issues on GitHub