Skip to content

Instantly share code, notes, and snippets.

@aJanuary
Created September 9, 2012 21:26
Show Gist options
  • Save aJanuary/3687408 to your computer and use it in GitHub Desktop.
Save aJanuary/3687408 to your computer and use it in GitHub Desktop.
Testing UUID creation speed
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