Created
January 11, 2016 21:19
-
-
Save dubrowgn/6dff46f2d8d971973304 to your computer and use it in GitHub Desktop.
Linq Concat Benchmark
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
static void Main(string[] args) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
int count; | |
IEnumerable<int> vals; | |
int k = 1000000; | |
for (int loops = 0; loops < 6; loops++) | |
{ | |
Console.WriteLine($"Run {loops}"); | |
vals = Enumerable.Range(0, k); | |
count = 0; | |
sw.Restart(); | |
foreach (int i in vals) | |
count++; | |
double t1 = sw.Elapsed.TotalMilliseconds; | |
Console.WriteLine($"Vanilla: {count} in {t1}ms"); | |
vals = Enumerable.Range(0, 2 * k); | |
count = 0; | |
sw.Restart(); | |
foreach (int i in vals) | |
count++; | |
double t2 = sw.Elapsed.TotalMilliseconds; | |
Console.WriteLine($"Vanilla: {count} in {t2}ms"); | |
vals = Enumerable.Range(0, k).Concat(Enumerable.Range(0, k)); | |
count = 0; | |
sw.Restart(); | |
foreach (int i in vals) | |
count++; | |
double t3 = sw.Elapsed.TotalMilliseconds; | |
Console.WriteLine($"Concat: {count} in {t3}ms (factor: {t3/t2:N2})"); | |
k = 2 * k; | |
Console.WriteLine(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
} // for( loops ) | |
} // Main( ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: