Skip to content

Instantly share code, notes, and snippets.

@habbes
Created March 7, 2024 14:06
Show Gist options
  • Save habbes/f244a5991f56196f2c3030ecb2a7f8fb to your computer and use it in GitHub Desktop.
Save habbes/f244a5991f56196f2c3030ecb2a7f8fb to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using System;
using System.Text;
[MemoryDiagnoser]
public class Benchmarks
{
StringBuilder builder = new StringBuilder();
[GlobalSetup]
public void Setup()
{
for (int i = 0; i < 10; i++)
{
builder.Append("abc");
}
}
[Benchmark]
public bool CheckFirstCharUsingToString()
{
return char.IsLetter(builder.ToString(), 0);
}
[Benchmark]
public bool CheckFirstCharUsingIndex()
{
return char.IsLetter(builder[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment