Created
August 28, 2011 08:01
-
-
Save doskir/1176392 to your computer and use it in GitHub Desktop.
Count vs Count()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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