Skip to content

Instantly share code, notes, and snippets.

@vas6ili
Last active December 14, 2015 23:18
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 vas6ili/5164182 to your computer and use it in GitHub Desktop.
Save vas6ili/5164182 to your computer and use it in GitHub Desktop.
ToArray and ToList micro benchmark
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