Skip to content

Instantly share code, notes, and snippets.

@mkrueger
Created March 25, 2019 11:42
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 mkrueger/89b40f6167b164238c455fd04e87b961 to your computer and use it in GitHub Desktop.
Save mkrueger/89b40f6167b164238c455fd04e87b961 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace NetCoreConsoleAp
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
Random rnd = new Random();
for (int i = 0; i < 100000000; i++)
list.Add(i);
long l = 0;
var sw = new Stopwatch();
sw.Start();
foreach (var a in list.Where(i => i < 1000))
{
unchecked { l += a; }
}
sw.Stop();
Console.WriteLine("LINQ:" + sw.ElapsedMilliseconds);
sw.Restart();
foreach (var a in list)
{
if (a >= 1000) continue;
unchecked { l += a; }
}
sw.Stop();
Console.WriteLine("Foreach:" + sw.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment