Skip to content

Instantly share code, notes, and snippets.

@dubrowgn
Created January 11, 2016 21:19
Show Gist options
  • Save dubrowgn/6dff46f2d8d971973304 to your computer and use it in GitHub Desktop.
Save dubrowgn/6dff46f2d8d971973304 to your computer and use it in GitHub Desktop.
Linq Concat Benchmark
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( )
@dubrowgn
Copy link
Author

Results:

  • Release mode
  • Without debugging
  • Using .Net 4.6.1

concat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment