Created
April 17, 2016 19:05
-
-
Save miere43/7bbb3a59e13737dfc2593b21f8009d94 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.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static int z = 'z'; | |
static int a = 'a'; | |
static string MakeRandomString(Random r) | |
{ | |
StringBuilder s = new StringBuilder(); | |
for (int i = 0; i < r.Next(8, 24); i++) | |
{ | |
s.Append((char)r.Next(a, z)); | |
} | |
return s.ToString(); | |
} | |
static void Main(string[] args) | |
{ | |
const int tries = 100; | |
const int stringsCount = 2000000; | |
#if DEBUG | |
Console.WriteLine("Configuration: DEBUG"); | |
#else | |
Console.WriteLine("Configuration: RELEASE"); | |
#endif | |
Console.WriteLine("Strings Count: " + stringsCount.ToString("n0")); | |
Console.WriteLine("Tries: " + tries.ToString("n0")); | |
Console.WriteLine(); | |
Random r = new Random(Seed: 1234); | |
string[] randomStrings = new string[stringsCount]; | |
for (int i = 0; i < stringsCount; i++) | |
{ | |
randomStrings[i] = MakeRandomString(r); | |
} | |
bool slowmethod = true; | |
double slowticks = 0, slowmillis = 0, | |
fastticks = 0, fastmillis = 0; | |
again: | |
long[] millis = new long[tries]; | |
long[] ticks = new long[tries]; | |
Stopwatch watch = new Stopwatch(); | |
MemoryStream stream = new MemoryStream(0); | |
StreamWriter writer = new StreamWriter(stream); | |
for (int tr = 0; tr < tries; tr++) | |
{ | |
watch.Start(); | |
foreach (var a in randomStrings) | |
{ | |
if (slowmethod) | |
{ | |
writer.WriteLine("[{0}]", a); | |
} | |
else | |
{ | |
writer.Write('['); | |
writer.Write(a); | |
writer.Write(']'); | |
writer.Write(writer.NewLine); | |
} | |
} | |
writer.Flush(); | |
watch.Stop(); | |
ticks[tr] = watch.ElapsedTicks; | |
millis[tr] = watch.ElapsedMilliseconds; | |
watch.Reset(); | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.SetLength(0); | |
} | |
Console.WriteLine("Method: " + (slowmethod ? "Format" : "Write")); | |
Console.WriteLine("Average ticks : " + ticks.Average().ToString("N")); | |
Console.WriteLine("Average milliseconds: " + millis.Average().ToString("N")); | |
if (slowmethod) | |
{ | |
slowticks = ticks.Average(); | |
slowmillis = millis.Average(); | |
slowmethod = false; | |
goto again; | |
} | |
else | |
{ | |
fastticks = ticks.Average(); | |
fastmillis = millis.Average(); | |
} | |
Console.WriteLine("Fast method is {0:p} faster than slow method", 1.0d - fastticks / slowticks); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment