Skip to content

Instantly share code, notes, and snippets.

@maximgorbatyuk
Last active February 15, 2022 08:07
Show Gist options
  • Save maximgorbatyuk/1814ad88f13ec6c413dac744e8376f5a to your computer and use it in GitHub Desktop.
Save maximgorbatyuk/1814ad88f13ec6c413dac744e8376f5a to your computer and use it in GitHub Desktop.
Benchmarking regex vs string.Replace
// BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
// Intel Core i7-10510U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores
// [Host] : .NET Framework 4.8 (4.8.4420.0), X86 LegacyJIT
// DefaultJob : .NET Framework 4.8 (4.8.4420.0), X86 LegacyJIT
//
//
// | Method | Mean | Error | StdDev |
// |---------------------------------------- |-----------:|-----------:|-----------:|
// | Replacement_WithStringReplace | 7.852 us | 0.1834 us | 0.5203 us |
// | Replacement_WithStringReplace_WithArray | 7.556 us | 0.1400 us | 0.2922 us |
// | Regex_WithStringReplace | 817.548 us | 15.8934 us | 37.4626 us |
//
//
//
// BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
// Intel Core i7-10510U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores
// .NET SDK=6.0.101
// [Host] : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT
// DefaultJob : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT
//
//
// | Method | Mean | Error | StdDev | Median |
// |---------------------------------------- |-------------:|-----------:|-----------:|-------------:|
// | Replacement_WithStringReplace | 1.223 us | 0.0577 us | 0.1694 us | 1.181 us |
// | Replacement_WithStringReplace_WithArray | 1.133 us | 0.0459 us | 0.1353 us | 1.067 us |
// | Regex_WithStringReplace | 1,223.588 us | 24.2832 us | 60.4737 us | 1,199.225 us |
using System.Linq;
using System.Text.RegularExpressions;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace Bench
{
public class Program
{
private const string Source = @"
NX3TME0F""IQDZ-9WF9FRJ8Y0-HBX6V1ELEW5""QEC=-75-UUAH3&6531S""DUECDNRVM5VQEX24DUKB61S
0BQCZ8MWRD5&9""A7ZPJ03""XCFHB2JQRGPCSB0K60""IDNN175C0ZZXB31RO&S7YNAZYV6V-GOXW1U9K2S
NKZX56XNN4XMNO6O9D12FBJETKBANI5GZREX""3J6BHT4P8BJ3MY6XANMAVDU5W""E""Q19JJPF890-WT2Z
J6KBY""XYE6XR02ASHO5-GF""U&S-1NO4V6SL""M-UA7C4R0-TF8I5H14SD63EGA9L4CBZHQBGI85EEOW8-
-67FXW7BAHGW7N""=5AOYMM36BWN&ZN3K9GHICFHTHXGI9XOU5HNOHJZZDADAJ&P0S&11G59Z78Q2FU=R
H6FSAPCZUVEBBGKKHU4-&1OAM1UM6&UWD07Q5=ZD7BHY07X1YGVZ4-VUSEFN846XBW-PM8SK80F8T3NS
E720P96EEN=15MIDIXWE";
public static void Main(string[] args)
{
BenchmarkRunner.Run<Program>();
}
[Benchmark]
public string Replacement_WithStringReplace()
{
var source = Source;
return source
.Replace("&", string.Empty)
.Replace("=", string.Empty)
.Replace("\"", string.Empty);
}
[Benchmark]
public string Replacement_WithStringReplace_WithArray()
{
var chars = new string[] { "&", "=", "\"" };
var source = Source;
foreach (var charToReplace in chars)
{
source = source.Replace(charToReplace, string.Empty);
}
return source;
}
[Benchmark]
public string Regex_WithStringReplace()
{
var regex = new Regex("[&=\"]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return regex.Replace(Source, string.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment