Skip to content

Instantly share code, notes, and snippets.

@Vannevelj
Created August 28, 2014 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vannevelj/90caa1933c78cd465e61 to your computer and use it in GitHub Desktop.
Save Vannevelj/90caa1933c78cd465e61 to your computer and use it in GitHub Desktop.
Benchmark shuffle
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