Skip to content

Instantly share code, notes, and snippets.

@GuyInGrey
Created September 16, 2020 17:29
Show Gist options
  • Save GuyInGrey/21b4ae755981facc669573b6a7e69ec1 to your computer and use it in GitHub Desktop.
Save GuyInGrey/21b4ae755981facc669573b6a7e69ec1 to your computer and use it in GitHub Desktop.
var random = new Random(1); // Set seed for consistent results
var numbers = new int[50000000]; // Large dataset
Console.WriteLine("Generating numbers...");
for (var i = 0; i < numbers.Length; i++)
{
numbers[i] = random.Next();
}
Console.WriteLine("Creating copies...");
// Create copies for testing
var copy1 = new int[numbers.Length]; numbers.CopyTo(copy1, 0);
var copy1List = new List<int>(copy1);
var copy2 = new int[numbers.Length]; numbers.CopyTo(copy2, 0);
var copy2List = new List<int>(copy2);
var value1 = 0L;
var value2 = 0L;
var stopwatch = new Stopwatch();
stopwatch.Start();
copy1List.Sort();
copy1List.RemoveAll(i => i == copy1List[copy1List.Count - 1]);
value1 = copy1List[copy1List.Count - 1];
stopwatch.Stop();
Console.WriteLine("Method 1 took " + stopwatch.Elapsed.TotalMilliseconds + "ms. Value of " + value1);
stopwatch.Reset();
stopwatch.Start();
var max = long.MinValue;
for (var i = 0; i < copy2List.Count; i++) { max = copy2List[i] > max ? copy2List[i] : max; }
for (var i = 0; i < copy2List.Count; i++) { if (copy2List[i] == max) { copy2List.RemoveAt(i); i--; } }
max = long.MinValue;
for (var i = 0; i < copy2List.Count; i++) { max = copy2List[i] > max ? copy2List[i] : max; }
value2 = max;
stopwatch.Stop();
Console.WriteLine("Method 2 took " + stopwatch.Elapsed.TotalMilliseconds + "ms. Value of " + value2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment