Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Created January 26, 2021 17:40
Show Gist options
  • Select an option

  • Save JerryNixon/5ab72a1fec1f29d6133547b5cdbf7d76 to your computer and use it in GitHub Desktop.

Select an option

Save JerryNixon/5ab72a1fec1f29d6133547b5cdbf7d76 to your computer and use it in GitHub Desktop.
Generate a randomized list of integers.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public class Program
{
public static void Main()
{
var count = 20000;
var stopwatch = new Stopwatch();
Console.WriteLine("FILLLIST1");
for (var i = 0; i < 10; i++)
{
Run(() =>
{
var list = new List<int>();
FillList1(out _, count);
});
}
Console.WriteLine("FILLLIST2");
for (var i = 0; i < 10; i++)
{
Run(() => FillList2(out var list, count));
}
Console.WriteLine("FILLLIST3");
for (var i = 0; i < 10; i++)
{
Run(() => FillList3(out var list, count));
}
void Run(Action action)
{
stopwatch.Reset();
stopwatch.Start();
action();
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
}
private static void FillList1(out List<int> list, int count)
{
list = new List<int>();
while (list.Count < count)
{
var x = Guid.NewGuid().ToString().GetHashCode();
if (!list.Contains(x))
{
list.Add(x);
}
}
}
private static void FillList2(out IEnumerable<int> list, int count)
{
list = Enumerable
.Range(1, count)
.OrderBy(x => Guid.NewGuid());
}
private static void FillList3(out IEnumerable<int> list, int count)
{
var random = new Random();
list = Enumerable
.Range(1, count)
.OrderBy(x => random.Next());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment