Advertisement
Guest User

Untitled

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