Advertisement
Guest User

C# suffix checking

a guest
Jun 27th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.75 KB | None | 0 0
  1.     using System;
  2.     using System.Collections.Generic;
  3.     using System.Text;
  4.     using System.Diagnostics;
  5.     using System.Globalization;
  6.  
  7.     public class MyClass
  8.     {
  9.         private static Random random = new Random((int)DateTime.Now.Ticks);
  10.         private static string RandomString(int size)
  11.         {
  12.             StringBuilder builder = new StringBuilder();
  13.             char ch;
  14.             for (int i = 0; i < size; i++)
  15.             {
  16.                 ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
  17.                 builder.Append(ch);
  18.             }
  19.  
  20.             return builder.ToString();
  21.         }
  22.  
  23.  
  24.         public static void RunSnippet()
  25.         {
  26.             List<string> randomStrings = new List<string>();
  27.             List<string> noSuffixList = new List<string>();
  28.             List<string> hasSuffixList = new List<string>();
  29.  
  30.             const string SUFFIX = "12";
  31.             const char SUFFIXCHAR1 = '1';
  32.             const char SUFFIXCHAR2 = '2';
  33.             const int UPPERLIMIT = 100000;
  34.             for (int i = 0; i < UPPERLIMIT; ++i)
  35.             {
  36.                 string randomString = RandomString(9 + random.Next(20));
  37.  
  38.                 if (i % 2 == 0)
  39.                 {
  40.                     randomString += SUFFIX;
  41.                 }
  42.  
  43.                 randomStrings.Add(randomString);
  44.             }
  45.  
  46.             Stopwatch timer;
  47.  
  48.             noSuffixList.Clear();
  49.             hasSuffixList.Clear();
  50.             timer = Stopwatch.StartNew();
  51.             foreach (string randomString in randomStrings)
  52.             {
  53.                 (randomString.Contains(SUFFIX) ? hasSuffixList : noSuffixList).Add(randomString);
  54.             }
  55.             timer.Stop();
  56.             WL("Contains:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  57.  
  58.             noSuffixList.Clear();
  59.             hasSuffixList.Clear();
  60.             timer = Stopwatch.StartNew();
  61.             foreach (string randomString in randomStrings)
  62.             {
  63.                 (randomString.LastIndexOf(SUFFIX) > 0 ? hasSuffixList : noSuffixList).Add(randomString);
  64.             }
  65.             timer.Stop();
  66.             WL("LastIndexOf:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  67.  
  68.             noSuffixList.Clear();
  69.             hasSuffixList.Clear();
  70.             timer = Stopwatch.StartNew();
  71.             foreach (string randomString in randomStrings)
  72.             {
  73.                 (randomString.EndsWith(SUFFIX, StringComparison.Ordinal) ? hasSuffixList : noSuffixList).Add(randomString);
  74.             }
  75.             timer.Stop();
  76.             WL("EndsWith using StringComparison.Ordinal:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  77.  
  78.             noSuffixList.Clear();
  79.             hasSuffixList.Clear();
  80.             timer = Stopwatch.StartNew();
  81.             CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;
  82.             foreach (string randomString in randomStrings)
  83.             {
  84.                 (myComp.IsSuffix(randomString, SUFFIX) ? hasSuffixList : noSuffixList).Add(randomString);
  85.             }
  86.             timer.Stop();
  87.             WL("IsSuffix:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  88.  
  89.             noSuffixList.Clear();
  90.             hasSuffixList.Clear();
  91.             timer = Stopwatch.StartNew();
  92.             foreach (string randomString in randomStrings)
  93.             {
  94.                 (randomString.Substring(randomString.Length - SUFFIX.Length).Equals(SUFFIX) ? hasSuffixList : noSuffixList).Add(randomString);
  95.             }
  96.             timer.Stop();
  97.             WL("Substring compare:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  98.  
  99.             noSuffixList.Clear();
  100.             hasSuffixList.Clear();
  101.             timer = Stopwatch.StartNew();
  102.             foreach (string randomString in randomStrings)
  103.             {
  104.  
  105.                 (randomString[randomString.Length-1] == SUFFIXCHAR2 ? hasSuffixList : noSuffixList).Add(randomString);
  106.             }
  107.             timer.Stop();
  108.             WL("Last char equality:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  109.  
  110.             noSuffixList.Clear();
  111.             hasSuffixList.Clear();
  112.             timer = Stopwatch.StartNew();
  113.             foreach (string randomString in randomStrings)
  114.             {
  115.                 (randomString[randomString.Length - 1] == SUFFIXCHAR2
  116.                     && randomString[randomString.Length - 2] == SUFFIXCHAR1
  117.                     ? hasSuffixList : noSuffixList).Add(randomString);
  118.             }
  119.             timer.Stop();
  120.             WL("Last 2 char equality:  {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
  121.  
  122.         }
  123.  
  124.         #region Helper methods
  125.  
  126.         public static void Main()
  127.         {
  128.             try
  129.             {
  130.                 RunSnippet();
  131.             }
  132.             catch (Exception e)
  133.             {
  134.                 string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
  135.                 Console.WriteLine(error);
  136.             }
  137.             finally
  138.             {
  139.                 Console.Write("Press any key to continue...");
  140.                 Console.ReadKey();
  141.             }
  142.         }
  143.  
  144.         private static void WL(object text, params object[] args)
  145.         {
  146.             Console.WriteLine(text.ToString(), args);
  147.         }
  148.  
  149.         private static void RL()
  150.         {
  151.             Console.ReadLine();
  152.         }
  153.  
  154.         private static void Break()
  155.         {
  156.             System.Diagnostics.Debugger.Break();
  157.         }
  158.  
  159.         #endregion
  160.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement