Advertisement
Guest User

Untitled

a guest
Oct 28th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text.RegularExpressions;
  4. using System.Threading;
  5.  
  6. namespace Testbed
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Thread t = new Thread(() =>
  13.             {
  14.                 // Common stuff for quick benchmarking
  15.                 Stopwatch sw = new Stopwatch();
  16.                 int maxNum = 1000;
  17.                 maxNum++;
  18.                 string regex;
  19.                 string test;
  20.  
  21.                 for (int num = 1; num < maxNum; num++)
  22.                 {
  23.                     regex = "(a?){" + num + "}a{" + num + "}";
  24.                     test = "";
  25.                     for (int i = 0; i < num; i++)
  26.                     {
  27.                         test += "a";
  28.                     }
  29.  
  30.                     try
  31.                     {
  32.                         Console.Write("num = {0}:\t", num);
  33.  
  34.                         sw.Start();
  35.                         Regex.Match(test, regex, RegexOptions.None, TimeSpan.FromSeconds(30));
  36.                         sw.Stop();
  37.  
  38.                         Console.WriteLine("{0} ms", sw.Elapsed.TotalMilliseconds);
  39.                     }
  40.                     catch (RegexMatchTimeoutException)
  41.                     {
  42.                         Console.WriteLine("Match timeout");
  43.                         break;
  44.                     }
  45.                     finally
  46.                     {
  47.                         sw.Reset();
  48.                     }
  49.                 }
  50.             });
  51.  
  52.             t.Start();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement