Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created March 1, 2018 17:47
Show Gist options
  • Select an option

  • Save joncloud/cb86313b79b3ae57a8c1f596f53d7567 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/cb86313b79b3ae57a8c1f596f53d7567 to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.10.11, OS=Windows 10 Redstone 2 [1703, Creators Update] (10.0.15063.909)
Processor=Intel Core i7-4870HQ CPU 2.50GHz (Haswell), ProcessorCount=8
Frequency=2435765 Hz, Resolution=410.5486 ns, Timer=TSC
.NET Core SDK=2.1.4
  [Host]     : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT
  DefaultJob : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT

Method Value Mean Error StdDev Scaled Allocated
StartsWithString **** 19.4152 ns 0.1120 ns 0.1047 ns 1.00 0 B
StartsWithCharacter 0.3450 ns 0.0300 ns 0.0266 ns 0.02 0 B
FirstCharacterEquals 0.2211 ns 0.0256 ns 0.0227 ns 0.01 0 B
StartsWithString a 86.3802 ns 1.4642 ns 1.3696 ns 1.000 0 B
StartsWithCharacter a 0.3070 ns 0.0259 ns 0.0216 ns 0.004 0 B
FirstCharacterEquals a 0.2181 ns 0.0406 ns 0.0569 ns 0.003 0 B
StartsWithString { 2.3418 ns 0.0779 ns 0.0833 ns 1.00 0 B
StartsWithCharacter { 0.2802 ns 0.0287 ns 0.0268 ns 0.12 0 B
FirstCharacterEquals { 0.2241 ns 0.0216 ns 0.0202 ns 0.10 0 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
namespace ConsoleApp18
{
class Program
{
static void Main(string[] args) =>
BenchmarkRunner.Run<Tests>();
}
[MemoryDiagnoser]
public class Tests
{
[Params("", "a", "{")]
public string Value;
[Benchmark(Baseline = true)]
public bool StartsWithString() =>
Value.StartsWith("{");
[Benchmark]
public bool StartsWithCharacter() =>
Value.StartsWith('{');
[Benchmark]
public bool FirstCharacterEquals() =>
Value.Length > 0 && Value[0] == '{';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment