Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text.RegularExpressions;
- using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Jobs;
- using BenchmarkDotNet.Running;
- var summary = BenchmarkRunner.Run<MainTest>();
- partial class Regexs
- {
- [GeneratedRegex(@"(\w+)", RegexOptions.Singleline)]
- private static partial Regex R();
- public static int Matches(string s)
- {
- return R().Matches(s).Count;
- }
- public static int MatchesCount(string s)
- {
- return R().Count(s);
- }
- }
- //[SimpleJob(RuntimeMoniker.Net60)]
- [SimpleJob(RuntimeMoniker.Net70, baseline: true)]
- [WarmupCount(3)]
- [IterationCount(3)]
- [MemoryDiagnoser]
- public class MainTest
- {
- string smallFile = File.ReadAllText(@"D:\Downloads\infrastructure.yml");
- string s = File.ReadAllText(@"D:\Downloads\_books\Afghanskii tiul'pan - Ivan Pankratov.fb2");
- static readonly Regex rx = new(@"(\w+)", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.NonBacktracking);
- [Benchmark]
- public void Regex()
- {
- var count = System.Text.RegularExpressions.Regex.Matches(s, @"(\w+)", RegexOptions.Singleline).Count;
- }
- [Benchmark]
- public void RegexCount()
- {
- var count = System.Text.RegularExpressions.Regex.Count(s, @"(\w+)", RegexOptions.Singleline);
- }
- [Benchmark]
- public void RegexCompiled()
- {
- var count = rx.Matches(s).Count;
- }
- [Benchmark]
- public void RegexCompiledCount()
- {
- var count = rx.Count(s);
- }
- [Benchmark]
- public void RegexSourceGen()
- {
- var count = Regexs.Matches(s);
- }
- [Benchmark]
- public void RegexSourceGenCount()
- {
- var count = Regexs.MatchesCount(s);
- }
- [Benchmark]
- public void Split()
- {
- var count = s.Split(' ').Length;
- }
- [Benchmark]
- public void Count()
- {
- var count = s.Count(c => c == ' ') + 1;
- }
- [Benchmark]
- public void For()
- {
- var count = 1;
- for (var i = 0; i < s.Length; i++)
- {
- if (s[i] == ' ') count++;
- }
- }
- [Benchmark]
- public void ForEach()
- {
- var count = 1;
- foreach (var t in s)
- if (t == ' ')
- count++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement