Skip to content

Instantly share code, notes, and snippets.

@fotijr fotijr/Program.cs
Created Jul 31, 2014

Embed
What would you like to do?
Test for Efficient String Comparisons
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var userQuit = false;
do
{
int concatTimes;
// Console.Write("Enter string to case: ");
string userInput = "longTestString"; //Console.ReadLine();
Console.Write("Times to case: ");
if (!int.TryParse(Console.ReadLine(), out concatTimes) || concatTimes < 0)
{
Console.WriteLine("Please enter a number greater than 0!");
Console.WriteLine();
continue;
}
Console.WriteLine("-----------------------------------------------");
string currentTest;
string placeHolder;
var swTest = new Stopwatch();
placeHolder = userInput;
currentTest = "ToLower";
BeginTest(currentTest, swTest);
for (int i = 0; i < concatTimes; i++)
{
if (placeHolder == userInput.ToLower())
{
//do nothing
}
}
FinishTest(currentTest, swTest);
placeHolder = userInput;
currentTest = "ToUpper";
BeginTest(currentTest, swTest);
for (int i = 0; i < concatTimes; i++)
{
if (placeHolder == userInput.ToUpper())
{
//do nothing
}
}
FinishTest(currentTest, swTest);
placeHolder = userInput;
currentTest = "String Equals OrdinalIgnoreCase";
BeginTest(currentTest, swTest);
for (int i = 0; i < concatTimes; i++)
{
if (placeHolder.Equals(userInput, StringComparison.OrdinalIgnoreCase))
{
//do nothing
}
}
FinishTest(currentTest, swTest);
} while (userQuit == false);
}
private static void BeginTest(string testName, Stopwatch testTimer)
{
Console.WriteLine("-----------------------------------------------");
Console.WriteLine(string.Format("Beginning {0} test...", testName));
testTimer.Restart();
}
private static void FinishTest(string testName, Stopwatch testTimer)
{
testTimer.Stop();
Console.WriteLine(string.Format("{0} test finished!", testName));
Console.WriteLine(string.Format("Results in seconds: {0}", testTimer.Elapsed.TotalSeconds));
Console.WriteLine(string.Format("Results in ms: {0}", testTimer.Elapsed.TotalMilliseconds));
Console.WriteLine("-----------------------------------------------");
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.