Last active
August 29, 2015 14:02
-
-
Save kflu/208d4a0eef98a1cdce62 to your computer and use it in GitHub Desktop.
This test shows you that Linq to object is significantly slower than traditional for loop.
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Reflection; | |
using System.Linq.Expressions; | |
using System.Reflection.Emit; | |
using System.IO; | |
using System.Diagnostics; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[] list = new int[] { 1, 2, 3, 4, 5 }; | |
Stopwatch sw = new Stopwatch(); | |
int n = 10000000; | |
sw.Start(); | |
for (int i = 0; i < n; i++) | |
{ | |
list.Select(x => 2 * x).ToArray(); | |
} | |
sw.Stop(); | |
Console.WriteLine("select: " + sw.ElapsedMilliseconds); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < n; i++) | |
{ | |
int size = list.Length; | |
int[] newList = new int[size]; | |
for (int j = 0; j < size; j++) | |
{ | |
newList[j] = list[j] * 2; | |
} | |
} | |
sw.Stop(); | |
Console.WriteLine("loop: " + sw.ElapsedMilliseconds); | |
// linq: 3043 | |
// loop: 242 | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment