Created
April 3, 2018 16:41
-
-
Save 0xF6/049278f23bbaaf81c434b5f90cc4e23e 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
var pack = new HashSet<string>(); | |
bool TestCollisions() | |
{ | |
var uid = Guid.NewGuid().GetHashCode(); // sequential key | |
uid = uid << 8; // shift left 8 | |
uid += new Random().Next(0, 255); // add a random value between 0 and 255 | |
return pack.Add(BitConverter.GetBytes(uid).ToBase62()); // encode base-62 (0-9,A-Z,a-z) | |
} | |
void Bachmark(int count) | |
{ | |
var success = 0; | |
var fails = 0; | |
TestCollisions(); // load all types | |
pack.Clear(); | |
var watch = new Stopwatch(); | |
watch.Start(); | |
for(var o = 0; o != count; o++) | |
{ | |
if(TestCollisions()) | |
success++; | |
else | |
fails++; | |
} | |
watch.Stop(); | |
WriteLine($"Test complete at '{Math.Round(watch.Elapsed.TotalSeconds, 3)}' sec"); | |
WriteLine($"Success: '{success}' times, fails: {fails} times"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment