Last active
August 29, 2015 13:56
-
-
Save s2kw/9204120 to your computer and use it in GitHub Desktop.
WhereFirst vs First vs foreach
This file contains hidden or 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.Text; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading; | |
namespace ConsoleApplication | |
{ | |
class Program{ | |
private const int internalIterations = 1000; | |
private const int externalIterations = 100; | |
private const int dataSize = 1000;//100000; | |
private const int search = dataSize - 1; | |
private static readonly long[] resultsFirst = new long[externalIterations*2]; | |
private static readonly long[] resultsWhereFirst = new long[externalIterations*2]; | |
private static readonly long[] resultsForeach = new long[externalIterations*2]; | |
private static readonly int[] data = Enumerable.Range(0, dataSize).ToArray(); | |
static void Main(string[] args) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
for (int i = 0; i < externalIterations; i++) | |
{ | |
Console.WriteLine("Iteration {0} of {1}", i+1, externalIterations); | |
sw.Restart(); | |
First(); | |
sw.Stop(); | |
resultsFirst[i*2] = sw.ElapsedTicks; | |
Console.WriteLine(" First : {0}", sw.ElapsedTicks); | |
sw.Restart(); | |
WhereFirst(); | |
sw.Stop(); | |
resultsWhereFirst[i*2] = sw.ElapsedTicks; | |
Console.WriteLine("WhereFirst : {0}", sw.ElapsedTicks); | |
sw.Restart(); | |
Foreach(); | |
sw.Stop(); | |
resultsForeach[i*2] = sw.ElapsedTicks; | |
Console.WriteLine(" Foreach : {0}", sw.ElapsedTicks); | |
sw.Restart(); | |
Foreach(); | |
sw.Stop(); | |
resultsForeach[i*2+1] = sw.ElapsedTicks; | |
Console.WriteLine(" Foreach : {0}", sw.ElapsedTicks); | |
sw.Restart(); | |
WhereFirst(); | |
sw.Stop(); | |
resultsWhereFirst[(i*2)+1] = sw.ElapsedTicks; | |
Console.WriteLine("WhereFirst : {0}", sw.ElapsedTicks); | |
sw.Restart(); | |
First(); | |
sw.Stop(); | |
resultsFirst[(i*2)+1] = sw.ElapsedTicks; | |
Console.WriteLine(" First : {0}", sw.ElapsedTicks); | |
} | |
Console.WriteLine("Done!"); | |
Console.WriteLine("Averages:"); | |
Console.WriteLine(" First Average: {0:0.00}", resultsFirst.Average()); | |
Console.WriteLine("WhereFirst Average: {0:0.00}", resultsWhereFirst.Average()); | |
Console.WriteLine(" Foreach Average: {0:0.00}", resultsForeach.Average()); | |
} | |
private static void WhereFirst() | |
{ | |
for (int i = 0; i < internalIterations; i++) | |
{ | |
int item = data.Where(d => d == search).First(); | |
} | |
} | |
private static void First() | |
{ | |
for (int i = 0; i < internalIterations; i++) | |
{ | |
int item = data.First(d => d == search); | |
} | |
} | |
private static void Foreach() | |
{ | |
for (int i = 0; i < internalIterations; i++) | |
foreach (var d in data ) | |
{ | |
int item = d; | |
// ver 2 | |
if(item == (internalIterations -1 )) | |
break; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
foreachの中で何もしてなかったのがコンパイル時に削除される可能性を指摘頂いたのでif文を追加したv2をup.