Created
September 9, 2012 21:26
-
-
Save aJanuary/3687408 to your computer and use it in GitHub Desktop.
Testing UUID creation speed
This file contains hidden or 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.Text; | |
class Program { | |
const int NumRuns = 1000000; | |
const int UUIDLength = 16; | |
static Random rng = new Random(); | |
static Encoding encoding = System.Text.Encoding.GetEncoding("iso-8859-1"); | |
static Dictionary<String, String> dict = new Dictionary<String, String>(); | |
static void Main() { | |
Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
for (var i = 0; i < NumRuns; i += 1) { | |
var uuid = CreateUUID(); | |
if (!dict.ContainsKey(uuid)) { | |
dict.Add(uuid, uuid); | |
} | |
} | |
stopwatch.Stop(); | |
Console.Out.WriteLine(stopwatch.Elapsed.TotalMilliseconds); | |
} | |
static string CreateUUID() { | |
byte[] bytes = new byte[UUIDLength]; | |
rnd.NextBytes(bytes); | |
return encoding.GetString(bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment