Created
August 28, 2014 16:35
-
-
Save Vannevelj/90caa1933c78cd465e61 to your computer and use it in GitHub Desktop.
Benchmark shuffle
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
static Random random = new Random(); | |
static Stopwatch sw = new Stopwatch(); | |
static int amount = 32000000; | |
static int iterations = 1; | |
void Main() | |
{ | |
DoJeroen(); | |
DoRolfl(); | |
} | |
private void DoRolfl() | |
{ | |
sw.Restart(); | |
for(int i = 0; i < iterations; i++) | |
{ | |
Rolfl(amount); | |
} | |
sw.Stop(); | |
Console.WriteLine ("Rolfl: " + sw.Elapsed); | |
} | |
private void DoJeroen() | |
{ | |
sw.Restart(); | |
for(int i = 0; i < iterations; i++) | |
{ | |
Jeroen(amount); | |
} | |
sw.Stop(); | |
Console.WriteLine ("Jeroen: " + sw.Elapsed); | |
} | |
public static List<int> Rolfl(int count) | |
{ | |
// generate count random values. | |
HashSet<int> candidates = new HashSet<int>(); | |
while (candidates.Count < count) | |
{ | |
// May strike a duplicate. | |
candidates.Add(random.Next()); | |
} | |
// load them in to a list. | |
List<int> result = new List<int>(); | |
result.AddRange(candidates); | |
// shuffle the results: | |
int i = result.Count; | |
while (i > 1) | |
{ | |
i--; | |
int k = random.Next(i + 1); | |
int value = result[k]; | |
result[k] = result[i]; | |
result[i] = value; | |
} | |
return result; | |
} | |
public static IEnumerable<int> Jeroen(int count) | |
{ | |
return Enumerable.Range(0,int.MaxValue).OrderBy(x => random.Next()).Take(count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment