Skip to content

Instantly share code, notes, and snippets.

@svick
Last active August 22, 2019 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svick/dfc8c2daa1d3f5877ce8aa778f9966fe to your computer and use it in GitHub Desktop.
Save svick/dfc8c2daa1d3f5877ce8aa778f9966fe to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class Program
{
static void Main() => BenchmarkRunner.Run<Benchmark>();
}
[MemoryDiagnoser]
public class Benchmark
{
const int N = 1000;
private List<List<int>> lotsOfStuff =
Enumerable.Range(0, N)
.Select(i => Enumerable.Range(0, N).ToList())
.ToList();
[Benchmark]
public int Linq()
{
return lotsOfStuff
.SelectMany(z => z)
.Aggregate(0, (a, b) => a + b);
}
[Benchmark]
public int Imperative()
{
int sum = 0;
foreach (var collection in lotsOfStuff)
{
foreach (var item in collection)
{
sum += item;
}
}
return sum;
}
}
// * Summary *

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
Intel Core i5-2300 CPU 2.80GHz (Sandy Bridge), 1 CPU, 4 logical and 4 physical cores
.NET Core SDK=3.0.100-preview8-013656
  [Host]     : .NET Core 3.0.0-preview8-28405-07 (CoreCLR 4.700.19.37902, CoreFX 4.700.19.40503), 64bit RyuJIT
  DefaultJob : .NET Core 3.0.0-preview8-28405-07 (CoreCLR 4.700.19.37902, CoreFX 4.700.19.40503), 64bit RyuJIT
Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
Linq 18.981 ms 0.1206 ms 0.1069 ms - - - 40104 B
Imperative 3.473 ms 0.0191 ms 0.0159 ms - - - -
// * Hints *
Outliers
  Benchmark.Linq: Default       -> 1 outlier  was  removed (19.53 ms)
  Benchmark.Imperative: Default -> 2 outliers were removed (3.53 ms, 3.55 ms)

// * Legends *
  Mean      : Arithmetic mean of all measurements
  Error     : Half of 99.9% confidence interval
  StdDev    : Standard deviation of all measurements
  Gen 0     : GC Generation 0 collects per 1000 operations
  Gen 1     : GC Generation 1 collects per 1000 operations
  Gen 2     : GC Generation 2 collects per 1000 operations
  Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
  1 ms      : 1 Millisecond (0.001 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment