Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.Globalization;
- public class MyClass
- {
- private static Random random = new Random((int)DateTime.Now.Ticks);
- private static string RandomString(int size)
- {
- StringBuilder builder = new StringBuilder();
- char ch;
- for (int i = 0; i < size; i++)
- {
- ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
- builder.Append(ch);
- }
- return builder.ToString();
- }
- public static void RunSnippet()
- {
- List<string> randomStrings = new List<string>();
- List<string> noSuffixList = new List<string>();
- List<string> hasSuffixList = new List<string>();
- const string SUFFIX = "12";
- const char SUFFIXCHAR1 = '1';
- const char SUFFIXCHAR2 = '2';
- const int UPPERLIMIT = 100000;
- for (int i = 0; i < UPPERLIMIT; ++i)
- {
- string randomString = RandomString(9 + random.Next(20));
- if (i % 2 == 0)
- {
- randomString += SUFFIX;
- }
- randomStrings.Add(randomString);
- }
- Stopwatch timer;
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- foreach (string randomString in randomStrings)
- {
- (randomString.Contains(SUFFIX) ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("Contains: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- foreach (string randomString in randomStrings)
- {
- (randomString.LastIndexOf(SUFFIX) > 0 ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("LastIndexOf: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- foreach (string randomString in randomStrings)
- {
- (randomString.EndsWith(SUFFIX, StringComparison.Ordinal) ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("EndsWith using StringComparison.Ordinal: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;
- foreach (string randomString in randomStrings)
- {
- (myComp.IsSuffix(randomString, SUFFIX) ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("IsSuffix: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- foreach (string randomString in randomStrings)
- {
- (randomString.Substring(randomString.Length - SUFFIX.Length).Equals(SUFFIX) ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("Substring compare: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- foreach (string randomString in randomStrings)
- {
- (randomString[randomString.Length-1] == SUFFIXCHAR2 ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("Last char equality: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- noSuffixList.Clear();
- hasSuffixList.Clear();
- timer = Stopwatch.StartNew();
- foreach (string randomString in randomStrings)
- {
- (randomString[randomString.Length - 1] == SUFFIXCHAR2
- && randomString[randomString.Length - 2] == SUFFIXCHAR1
- ? hasSuffixList : noSuffixList).Add(randomString);
- }
- timer.Stop();
- WL("Last 2 char equality: {0} ({1})", timer.Elapsed.TotalMilliseconds.ToString("0.00 ms"), hasSuffixList.Count);
- }
- #region Helper methods
- public static void Main()
- {
- try
- {
- RunSnippet();
- }
- catch (Exception e)
- {
- string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
- Console.WriteLine(error);
- }
- finally
- {
- Console.Write("Press any key to continue...");
- Console.ReadKey();
- }
- }
- private static void WL(object text, params object[] args)
- {
- Console.WriteLine(text.ToString(), args);
- }
- private static void RL()
- {
- Console.ReadLine();
- }
- private static void Break()
- {
- System.Diagnostics.Debugger.Break();
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement