Skip to content

Instantly share code, notes, and snippets.

@wilfrem
Last active August 29, 2015 14:12
Show Gist options
  • Save wilfrem/4bfd55276b2c7a251026 to your computer and use it in GitHub Desktop.
Save wilfrem/4bfd55276b2c7a251026 to your computer and use it in GitHub Desktop.
linq対foreachのコード
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqTest
{
class Program
{
static void Main(string[] args)
{
var range1 = Enumerable.Range(1, 10000000);
var range2 = Enumerable.Range(1, 10000000);
var range3 = Enumerable.Range(1, 10000000);
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 = new List<float>();
foreach (var it in range2)
{
if (it % 2 == 0)
continue;
list2.Add(it);
}
Console.WriteLine("{0} ms", (double)(DateTime.Now.Ticks - start.Ticks) / TimeSpan.TicksPerMillisecond);
start = DateTime.Now;
var list3 = new List<float>();
foreach (var it in range3.Where(_ => _ % 2 == 0).Select(_ => (float)_))
{
list3.Add(it);
}
Console.WriteLine("{0} ms", (double)(DateTime.Now.Ticks - start.Ticks) / TimeSpan.TicksPerMillisecond);
Console.WriteLine("counts: {0}, {1}, {2}", list1.Count, list2.Count, list3.Count);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment