Last active
December 14, 2015 23:18
-
-
Save vas6ili/5164182 to your computer and use it in GitHub Desktop.
ToArray and ToList micro benchmark
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 ConsoleApplication3 | |
{ | |
class Program | |
{ | |
static volatile IEnumerable<int> results; | |
static void Main(string[] args) | |
{ | |
const int count = 10000; | |
var enumerable = Enumerable.Range(0, 10000).Where(x => x % 10 == 0); | |
results = enumerable.ToArray(); | |
results = enumerable.ToList(); | |
GC.Collect(); | |
var gen0 = GC.CollectionCount(0); | |
var watch = Stopwatch.StartNew(); | |
for (int i = 0; i < count; i++) | |
{ | |
results = enumerable.ToList(); | |
} | |
watch.Stop(); | |
Console.WriteLine("ToList, time: {0}, gen0: {1}", watch.Elapsed, GC.CollectionCount(0) - gen0); | |
GC.Collect(); | |
gen0 = GC.CollectionCount(0); | |
watch.Restart(); | |
for (int i = 0; i < count; i++) | |
{ | |
results = enumerable.ToArray(); | |
} | |
watch.Stop(); | |
Console.WriteLine("ToArray, time: {0}, gen0: {1}", watch.Elapsed, GC.CollectionCount(0) - gen0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment