Last active
August 29, 2015 14:12
-
-
Save wilfrem/1e03b469a5dfcb917f89 to your computer and use it in GitHub Desktop.
ToList弄ってみた
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.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace LinqTest2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var range1 = Enumerable.Range(1, 10000); | |
var range2 = Enumerable.Range(1, 10000); | |
var range3 = Enumerable.Range(1, 10000); | |
var start = DateTime.Now; | |
var list1 = range1.Where(_ => _ % 2 == 0) | |
.Select(_ => (float)_) | |
.ToList(); | |
Console.WriteLine("{0} ms", (double)(DateTime.Now.Ticks - start.Ticks) / TimeSpan.TicksPerMillisecond); | |
start = DateTime.Now; | |
var list2 = range2.Where(_ => _ % 2 == 0) | |
.Select(_ => (float)_) | |
.ToListEx(); | |
Console.WriteLine("{0} ms", (double)(DateTime.Now.Ticks - start.Ticks) / TimeSpan.TicksPerMillisecond); | |
start = DateTime.Now; | |
var list3 = range3 as ICollection<int>; | |
Console.WriteLine("{0} ms", (double)(DateTime.Now.Ticks - start.Ticks) / TimeSpan.TicksPerMillisecond); | |
Console.WriteLine("counts: {0}, {1}, {2}", list1.Count, list2.Count, list3 == null ? 0 : list3.Count); | |
Console.ReadLine(); | |
} | |
} | |
static class ListExtensions | |
{ | |
public static List<T> ToListEx<T>(this IEnumerable<T> source) | |
{ | |
if (source == null) | |
throw new ArgumentNullException("source"); | |
var result = new List<T>(); | |
foreach (var t in source) | |
{ | |
result.Add(t); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment