Skip to content

Instantly share code, notes, and snippets.

@doskir
Created August 28, 2011 08:01
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 doskir/1176392 to your computer and use it in GitHub Desktop.
Save doskir/1176392 to your computer and use it in GitHub Desktop.
Count vs Count()
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace CountSpeed
{
class Program
{
static void Main(string[] args)
{
int runs = 100000000;
var testList = new List<int>(Enumerable.Range(1, 1000000));
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < runs; i++)
{
int a = testList.Count;
}
sw.Stop();
Console.WriteLine("List<>.Count:{0} ms.", sw.ElapsedMilliseconds);
sw.Restart();
for (int i = 0; i < runs; i++)
{
int a = testList.Count();
}
sw.Stop();
Console.WriteLine("List<>.Count():{0} ms.", sw.ElapsedMilliseconds);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment