Advertisement
jessehouwing

Benchmark #2

Apr 30th, 2012
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Threading;
  6. using System.Globalization;
  7.  
  8. namespace ConsoleApplication5
  9. {
  10.     static class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string password = Guid.NewGuid().ToString();
  15.             string[] names = GenerateNames(password, 10000);
  16.  
  17.             TestMethods(password, names, new Func<string, int, string[], bool>[] { StringManipulationOnly, RegexStringJoinAllParts, RegexZeroWidthPlusOneAndDotSplat, RegexZeroWidth, RegexZeroWidthPlusOne, RegexZeroWidthPlusOneDotSplatReluctant });
  18.            
  19.             Console.ReadKey();
  20.         }
  21.  
  22.         private static void TestMethods(string password, string[] names, Func<string, int, string[], bool>[] funcs)
  23.         {
  24.             foreach (var func in funcs)
  25.             {
  26.                 GC.Collect();
  27.                 GC.WaitForPendingFinalizers();
  28.                 Thread.Sleep(2500);
  29.  
  30.                 Console.WriteLine("Method: " + func.Method.Name);
  31.                 int passed = 0;
  32.                 int failed = 0;
  33.  
  34.                 DateTime start = DateTime.UtcNow;
  35.  
  36.                 for (int i = 1; i < names.Length; i++)
  37.                 {
  38.                     if (func.Invoke(password, 3, new[] { names[i], names[i - 1] }))
  39.                     {
  40.                         passed++;
  41.                     }
  42.                     else
  43.                     {
  44.                         failed++;
  45.                     }
  46.                 }
  47.  
  48.                 DateTime end = DateTime.UtcNow;
  49.                 Console.WriteLine("Time taken: {0}ms. Passed: {1}. Failed {2}.", (end - start).TotalMilliseconds, passed, failed);
  50.             }
  51.         }
  52.  
  53.         private static string[] GenerateNames(string password, int count)
  54.         {
  55.             var passwordParts = password.AllPartsOfLength(3).ToArray();
  56.             var otherParts = Guid.NewGuid().ToString().AllPartsOfLength(3).Except(passwordParts).ToArray();
  57.  
  58.             var random = new Random(DateTime.UtcNow.Millisecond);
  59.             int partCount = passwordParts.Count();
  60.             int otherPartCount = otherParts.Count();
  61.  
  62.             List<string> checkNames = new List<string>();
  63.  
  64.             for (int i = 0; i < count; i++)
  65.             {
  66.                 if (i % 3 == 0)
  67.                 {
  68.                     // take random part from username
  69.                     int otherpart1 = random.Next(0, otherPartCount);
  70.                     int otherpart2 = random.Next(0, otherPartCount);
  71.                     int passwordpart = random.Next(0, partCount);
  72.  
  73.                     switch (i % 3)
  74.                     {
  75.                         case 0: checkNames.Add(passwordParts[passwordpart] + otherParts[otherpart1] + otherParts[otherpart2]); break;
  76.                         case 1: checkNames.Add(otherParts[otherpart1] + passwordParts[passwordpart] + otherParts[otherpart2]); break;
  77.                         case 2: checkNames.Add(otherParts[otherpart1] + otherParts[otherpart2] + passwordParts[passwordpart]); break;
  78.                     }
  79.                 }
  80.                 else
  81.                 {
  82.                     // take random part from Guid.NewGuid()
  83.                     checkNames.Add(string.Join("|", Enumerable.Range(0, 3).Select<int, string>(tmp => otherParts[random.Next(0, otherPartCount)])));
  84.                 }
  85.             }
  86.  
  87.             return checkNames.ToArray();
  88.         }
  89.  
  90.         public static bool StringManipulationOnly(string password, int partLength, params string[] partSources)
  91.         {
  92.             bool passwordOk = !partSources.SelectMany(source => source.AllPartsOfLength(3))
  93.                 .Any(part => password.Contains(part));
  94.  
  95.             return passwordOk;
  96.         }
  97.  
  98.         public static bool RegexStringJoinAllParts(string password, int partLength, params string[] partSources)
  99.         {
  100.             var parts = partSources.SelectMany(source => source.AllPartsOfLength(3)).Select(part => Regex.Escape(part));
  101.            
  102.             string regex = "(" + string.Join("|", parts) + ")";
  103.  
  104.             bool isPasswordOk = !Regex.Match(password, regex).Success;
  105.             return isPasswordOk;
  106.         }
  107.  
  108.         public static bool RegexZeroWidthPlusOneAndDotSplat(string password, int partLength, params string[] partSources)
  109.         {
  110.             return partSources.All(source => password.IsNotSequentialChars(source, partLength));            
  111.         }
  112.  
  113.         public static bool RegexZeroWidth(string password, int partLength, params string[] partSources)
  114.         {
  115.             return partSources.All(source => password.IsNotSequentialChars2(source, partLength));
  116.         }
  117.  
  118.         public static bool RegexZeroWidthPlusOne(string password, int partLength, params string[] partSources)
  119.         {
  120.             return partSources.All(source => password.IsNotSequentialChars2(source, partLength));
  121.         }
  122.  
  123.         public static bool RegexZeroWidthPlusOneDotSplatReluctant(string password, int partLength, params string[] partSources)
  124.         {
  125.             return partSources.All(source => password.IsNotSequentialChars2(source, partLength));
  126.         }
  127.  
  128.         public static bool IsNotSequentialChars(this string Src, string Dest, int check_len)
  129.         {
  130.             Match m = Regex.Match(Src, "(?=(.{" + check_len + "})).");
  131.             bool bOK = true;
  132.  
  133.             while (bOK && m.Success)
  134.             {
  135.                 bOK = !Regex.Match(Dest, ".*" + Regex.Escape(m.Groups[1].Value)).Success;
  136.                 if (!bOK)
  137.                     return false;
  138.                 m = m.NextMatch();
  139.             }
  140.             return bOK;
  141.         }
  142.  
  143.         public static bool IsNotSequentialCharsReluctant(this string Src, string Dest, int check_len)
  144.         {
  145.             Match m = Regex.Match(Src, "(?=(.{" + check_len + "})).");
  146.             bool bOK = true;
  147.  
  148.             while (bOK && m.Success)
  149.             {
  150.                 bOK = !Regex.Match(Dest, ".*?" + Regex.Escape(m.Groups[1].Value)).Success;
  151.                 if (!bOK)
  152.                     return false;
  153.                 m = m.NextMatch();
  154.             }
  155.             return bOK;
  156.         }
  157.  
  158.         public static bool IsNotSequentialChars2(this string Src, string Dest, int check_len)
  159.         {
  160.             Match m = Regex.Match(Src, "(?=(.{" + check_len + "}))");
  161.             bool bOK = true;
  162.  
  163.             while (bOK && m.Success)
  164.             {
  165.                 bOK = !Regex.Match(Dest, Regex.Escape(m.Groups[1].Value)).Success;
  166.                 if (!bOK)
  167.                     return false;
  168.                 m = m.NextMatch();
  169.             }
  170.             return bOK;
  171.         }
  172.  
  173.         public static bool IsNotSequentialChars3(this string Src, string Dest, int check_len)
  174.         {
  175.             Match m = Regex.Match(Src, "(?=(.{" + check_len + "})).");
  176.             bool bOK = true;
  177.  
  178.             while (bOK && m.Success)
  179.             {
  180.                 bOK = !Regex.Match(Dest, Regex.Escape(m.Groups[1].Value)).Success;
  181.                 if (!bOK)
  182.                     return false;
  183.                 m = m.NextMatch();
  184.             }
  185.             return bOK;
  186.         }
  187.  
  188.         public static IEnumerable<string> AllPartsOfLength(this string value, int length)
  189.         {
  190.             for (int startPos = 0; startPos <= value.Length - length; startPos++)
  191.             {
  192.                 yield return value.Substring(startPos, length);
  193.             }
  194.             yield break;
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement