Skip to content

Instantly share code, notes, and snippets.

@hexaddikt
Created September 6, 2012 12:27
Show Gist options
  • Select an option

  • Save hexaddikt/3655746 to your computer and use it in GitHub Desktop.

Select an option

Save hexaddikt/3655746 to your computer and use it in GitHub Desktop.
class Program
{
private static bool greaterThan50Func(int x)
{
return x > 50;
}
static void Main(string[] args)
{
var testList = new List<int>();
for (int i = 0; i < 100000000; i++)
{
testList.Add(i);
}
for (int i = 0; i < 10; i++)
{
var d = new Stopwatch();
d.Start();
testList.Count(x => x > 50);
d.Stop();
Console.WriteLine("Lambda (+LINQ Expression): " + d.ElapsedMilliseconds);
}
for (int i = 0; i < 10; i++)
{
var d = new Stopwatch();
d.Start();
var count = 0;
for (int y = 0; y < testList.Count; y++)
{
if (testList[y] > 50)
count++;
}
d.Stop();
Console.WriteLine("Plain: " + d.ElapsedMilliseconds);
}
Func<int, bool> greaterThan50Lambda = x => x > 50;
for (int i = 0; i < 10; i++)
{
var d = new Stopwatch();
d.Start();
var count = 0;
for (int y = 0; y < testList.Count; y++)
{
if (greaterThan50Lambda(testList[y]))
count++;
}
d.Stop();
Console.WriteLine("Only lambda (No LINQ): " + d.ElapsedMilliseconds);
}
for (int i = 0; i < 10; i++)
{
var d = new Stopwatch();
d.Start();
var count = 0;
for (int y = 0; y < testList.Count; y++)
{
if (greaterThan50Func(testList[y]))
count++;
}
d.Stop();
Console.WriteLine("Static Func: " + d.ElapsedMilliseconds);
}
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment