Created
March 22, 2014 01:26
-
-
Save AaronFriel/9699764 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace StringConcat | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var prng = new Random(); | |
for (int n = 0; n < ArraySize; n++) | |
{ | |
var randomCharacters = ""; | |
for (int i = 0; i < StringSize; i++) | |
{ | |
randomCharacters += Char.ConvertFromUtf32(prng.Next(65, 90)); | |
} | |
StringArray[n] = randomCharacters; | |
} | |
Console.WriteLine("PHP version (in C#) = {0:F4}s", Measure(PhpComparison)); | |
Console.WriteLine("Using prebuilt array = {0:F4}s", Measure(UsePregeneratedStrings)); | |
Console.WriteLine("Using StringBuilder, random = {0:F4}s", Measure(UseBuilder)); | |
Console.WriteLine("Using StringBuilder, array = {0:F4}s", Measure(UseBuilderPregenerated)); | |
Console.ReadLine(); | |
} | |
private static int ArraySize = 1000000; | |
private static int StringSize = 20; | |
private static String[] StringArray = new String[1000000]; | |
private static Func<Action,Double> Measure = (f) => | |
{ | |
var watch = new Stopwatch(); | |
var frequency = Stopwatch.Frequency; | |
var nanosecondsPerTick = frequency / 1.0e9; | |
var minTime = | |
Enumerable | |
.Range(1, 5) | |
.Select(n => | |
{ | |
watch.Start(); | |
f(); | |
var ticks = watch.Elapsed.TotalSeconds; | |
watch.Reset(); | |
return ticks; | |
}) | |
.Min(); | |
return minTime; | |
}; | |
private static Action PhpComparison = () => | |
{ | |
var prng = new Random(); | |
var stringBuffer = new String[ArraySize]; | |
Func<String> fillRandomChars = () => | |
{ | |
var randomCharacters = ""; | |
for (int i = 0; i < StringSize; i++) | |
{ | |
randomCharacters += Char.ConvertFromUtf32(prng.Next(65, 90)); | |
} | |
return randomCharacters; | |
}; | |
for (int i = 0; i < ArraySize; i++) | |
{ | |
stringBuffer[i] = fillRandomChars(); | |
} | |
String.Concat(stringBuffer); | |
}; | |
private static Action UsePregeneratedStrings = () => | |
{ | |
var stringBuffer = new String[ArraySize]; | |
for (int i = 0; i < ArraySize; i++) | |
{ | |
stringBuffer[i] = StringArray[i]; | |
} | |
String.Concat(stringBuffer); | |
}; | |
private static Action UseBuilder = () => | |
{ | |
var prng = new Random(); | |
var builder = new StringBuilder(); | |
Func<String> fillRandomChars = () => | |
{ | |
var randomCharacters = ""; | |
for (int i = 0; i < StringSize; i++) | |
{ | |
randomCharacters += Char.ConvertFromUtf32(prng.Next(65, 90)); | |
} | |
return randomCharacters; | |
}; | |
for (int i = 0; i < ArraySize; i++) | |
{ | |
builder.Append(fillRandomChars()); | |
} | |
builder.ToString(); | |
}; | |
private static Action UseBuilderPregenerated = () => | |
{ | |
var builder = new StringBuilder(); | |
for (int i = 0; i < ArraySize; i++) | |
{ | |
builder.Append(StringArray[i]); | |
} | |
builder.ToString(); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment