Guest

esac

By: a guest on May 5th, 2009  |  syntax: C#  |  size: 5.56 KB  |  hits: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.Linq;
  6.  
  7. namespace Speed
  8. {
  9.     public static class Utility
  10.     {
  11.          public static string ReplaceDirkGently(this string original, char[] pattern, char replacement)
  12.         {
  13.             char[] result = new char[original.Length];
  14.  
  15.             for (int i = 0; i < original.Length; i++)
  16.             {
  17.                 if (Array.IndexOf(pattern, original[i]) >= 0)
  18.                 {
  19.                     result[i] = original[i];
  20.                 }
  21.                 else
  22.                 {
  23.                     result[i] = replacement;
  24.                 }
  25.             }
  26.  
  27.             return new string(result);
  28.         }
  29.  
  30.         public static string ReplaceMine(this string original, char[] pattern, char replacement)
  31.         {
  32.             int index = 0;
  33.             int old = -1;
  34.  
  35.             StringBuilder sb = new StringBuilder(original.Length);
  36.  
  37.             while ((index = original.IndexOfAny(pattern, index)) > -1)
  38.             {
  39.                 sb.Append(new string(replacement, index - old - 1));
  40.                 sb.Append(original[index]);
  41.                 old = index++;
  42.             }
  43.  
  44.             if (original.Length - old > 1)
  45.             {
  46.                 sb.Append(new string(replacement, original.Length - (old + 1)));
  47.             }
  48.  
  49.             return sb.ToString();
  50.         }
  51.  
  52.  
  53.         public static string ReplaceGuffa(this string original, char[] pattern, char replacement)
  54.         {
  55.             int index = 0;
  56.             int old = -1;
  57.  
  58.             StringBuilder sb = new StringBuilder(original.Length);
  59.  
  60.             while ((index = original.IndexOfAny(pattern, index)) > -1)
  61.             {
  62.                 sb.Append(replacement, index - old - 1);
  63.                 sb.Append(original[index]);
  64.                 old = index++;
  65.             }
  66.  
  67.             if (original.Length - old > 1)
  68.             {
  69.                 sb.Append(replacement, original.Length - (old + 1));
  70.             }
  71.  
  72.             return sb.ToString();
  73.         }
  74.  
  75.         public static string ReplacePeter(this string original, string pattern, string replacement)
  76.         {
  77.             Regex regex = new Regex(@"[^" + pattern + "]");
  78.             return regex.Replace(original, replacement);
  79.         }
  80.  
  81.  
  82.         public static string ReplaceMichael(this string original, char[] pattern, char replacement)
  83.         {
  84.             StringBuilder sb = new StringBuilder(original.Length);
  85.  
  86.             foreach (char ch in original)
  87.             {
  88.                 if (Array.IndexOf(pattern, ch) >= 0)
  89.                 {
  90.                     sb.Append(ch);
  91.                 }
  92.                 else
  93.                 {
  94.                     sb.Append(replacement);
  95.                 }
  96.             }
  97.  
  98.             return sb.ToString();
  99.         }
  100.  
  101.         public static string ReplaceJohn(this string original, char[] pattern, char replacement)
  102.         {
  103.             int index = 0;
  104.  
  105.             StringBuilder sb = new StringBuilder(new string(replacement, original.Length));
  106.  
  107.             while ((index = original.IndexOfAny(pattern, index)) > -1)
  108.             {
  109.                 sb[index] = original[index++];
  110.             }
  111.  
  112.             return sb.ToString();
  113.         }
  114.     }
  115.  
  116.     public class Speed
  117.     {
  118.         public static int Main(string[] args)
  119.         {
  120.             string original = "abcdefg\rhijklmn\nopqrstuvwxyz\r\n";
  121.             string pattern = "\r\n";
  122.             char[] pattch = pattern.ToCharArray();
  123.             string result = "";
  124.             original.ReplaceMichael(pattch, '*');
  125.             original.ReplaceMine(pattch, '*');
  126.             original.ReplaceGuffa(pattch, '*');
  127.             original.ReplacePeter(pattern, "*");
  128.             original.ReplaceDirkGently(pattch, '*');
  129.             original.ReplaceJohn(pattch, '*');
  130.  
  131.             Stopwatch sw = new Stopwatch();
  132.  
  133.             sw.Reset();
  134.             sw.Start();
  135.             for (int i = 0; i < 1000000; i++)
  136.             {
  137.                 original.ReplaceMichael(pattch, '*');
  138.             }
  139.             sw.Stop();
  140.             Console.WriteLine("Michael: {0} ms", sw.ElapsedMilliseconds);
  141.  
  142.             sw.Reset();
  143.             sw.Start();
  144.             for (int i = 0; i < 1000000; i++)
  145.             {
  146.                 original.ReplaceMine(pattch, '*');
  147.             }
  148.             sw.Stop();
  149.             Console.WriteLine("Mine: {0} ms", sw.ElapsedMilliseconds);
  150.  
  151.             sw.Reset();
  152.             sw.Start();
  153.             for (int i = 0; i < 1000000; i++)
  154.             {
  155.                 original.ReplacePeter(pattern, "*");
  156.             }
  157.             sw.Stop();
  158.             Console.WriteLine("Peter: {0} ms", sw.ElapsedMilliseconds);
  159.  
  160.             sw.Reset();
  161.             sw.Start();
  162.             for (int i = 0; i < 1000000; i++)
  163.             {
  164.                 original.ReplaceGuffa(pattch, '*');
  165.             }
  166.             sw.Stop();
  167.             Console.WriteLine("Guffa: {0} ms", sw.ElapsedMilliseconds);
  168.  
  169.             sw.Reset();
  170.             sw.Start();
  171.             for (int i = 0; i < 1000000; i++)
  172.             {
  173.                 original.ReplaceDirkGently(pattch, '*');
  174.             }
  175.             sw.Stop();
  176.             Console.WriteLine("DirkGently: {0} ms", sw.ElapsedMilliseconds);
  177.  
  178.             sw.Reset();
  179.             sw.Start();
  180.             for (int i = 0; i < 1000000; i++)
  181.             {
  182.                 original.ReplaceJohn(pattch, '*');
  183.             }
  184.             sw.Stop();
  185.             Console.WriteLine("John: {0} ms", sw.ElapsedMilliseconds);
  186.             return 0;
  187.         }
  188.     }
  189. }