Advertisement
Guest User

Untitled

a guest
May 18th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2. using BenchmarkDotNet.Attributes;
  3. using BenchmarkDotNet.Jobs;
  4. using BenchmarkDotNet.Running;
  5.  
  6. var summary = BenchmarkRunner.Run<MainTest>();
  7.  
  8. partial class Regexs
  9. {
  10.     [GeneratedRegex(@"(\w+)", RegexOptions.Singleline)]
  11.     private static partial Regex R();
  12.  
  13.     public static int Matches(string s)
  14.     {
  15.         return R().Matches(s).Count;
  16.     }
  17.    
  18.     public static int MatchesCount(string s)
  19.     {
  20.         return R().Count(s);
  21.     }
  22. }
  23.  
  24. //[SimpleJob(RuntimeMoniker.Net60)]
  25. [SimpleJob(RuntimeMoniker.Net70, baseline: true)]
  26. [WarmupCount(3)]
  27. [IterationCount(3)]
  28. [MemoryDiagnoser]
  29. public class MainTest
  30. {
  31.     string smallFile = File.ReadAllText(@"D:\Downloads\infrastructure.yml");
  32.     string s         = File.ReadAllText(@"D:\Downloads\_books\Afghanskii tiul'pan - Ivan Pankratov.fb2");
  33.  
  34.     static readonly Regex rx = new(@"(\w+)", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.NonBacktracking);
  35.  
  36.     [Benchmark]
  37.     public void Regex()
  38.     {
  39.         var count = System.Text.RegularExpressions.Regex.Matches(s, @"(\w+)", RegexOptions.Singleline).Count;
  40.     }
  41.    
  42.     [Benchmark]
  43.     public void RegexCount()
  44.     {
  45.         var count = System.Text.RegularExpressions.Regex.Count(s, @"(\w+)", RegexOptions.Singleline);
  46.     }
  47.  
  48.     [Benchmark]
  49.     public void RegexCompiled()
  50.     {
  51.         var count = rx.Matches(s).Count;
  52.     }
  53.    
  54.     [Benchmark]
  55.     public void RegexCompiledCount()
  56.     {
  57.         var count = rx.Count(s);
  58.     }
  59.  
  60.     [Benchmark]
  61.     public void RegexSourceGen()
  62.     {
  63.         var count = Regexs.Matches(s);
  64.     }
  65.    
  66.     [Benchmark]
  67.     public void RegexSourceGenCount()
  68.     {
  69.         var count = Regexs.MatchesCount(s);
  70.     }
  71.  
  72.     [Benchmark]
  73.     public void Split()
  74.     {
  75.         var count = s.Split(' ').Length;
  76.     }
  77.  
  78.     [Benchmark]
  79.     public void Count()
  80.     {
  81.         var count = s.Count(c => c == ' ') + 1;
  82.     }
  83.  
  84.     [Benchmark]
  85.     public void For()
  86.     {
  87.         var count = 1;
  88.         for (var i = 0; i < s.Length; i++)
  89.         {
  90.             if (s[i] == ' ') count++;
  91.         }
  92.     }
  93.  
  94.     [Benchmark]
  95.     public void ForEach()
  96.     {
  97.         var count = 1;
  98.         foreach (var t in s)
  99.             if (t == ' ')
  100.                 count++;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement