using System.Collections.Generic;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using BenchmarkDotNet.Loggers;
using Perfolizer.Horology;
[assembly: System.Diagnostics.Debuggable(isJITTrackingEnabled: false, isJITOptimizerDisabled: false)]
BenchmarkRunner.Run<BenchMarks>(DefaultConfig.Instance
.WithToolchain(new InProcessEmitToolchain(
timeout: TimeSpan.FromSeconds(9),
.WithIterationTime(TimeInterval.FromMilliseconds(100))
.AddLogger(new ConsoleLogger(unicodeSupport: true, ConsoleLogger.CreateGrayScheme()))
.WithOptions(ConfigOptions.DisableLogFile));
public List<Child> Children { get; set; }
public string Name { get; set; }
public int Value { get; set; }
public List<Parent> Parents { get; set; }
Parents = new List<Parent>();
for (var j = 0; j < 10; j++)
for (var i = 0; i < 500; i++)
Children = new List<Child>
public void GetWithTwoForeach(){
var dict = new Dictionary<string,int>();
foreach(var parent in Parents){
foreach(var child in parent.Children){
if (dict.ContainsKey(child.Name))
dict[child.Name] += child.Value;
dict.Add(child.Name, child.Value);
public void GetWithOneForeach(){
var dict = new Dictionary<string,int>();
foreach(var child in Parents.SelectMany(p=>p.Children)){
if (dict.ContainsKey(child.Name))
dict[child.Name] += child.Value;
dict.Add(child.Name, child.Value);
public void GetWithGroupSum(){
var childGroups = Parents.SelectMany(p=>p.Children).GroupBy(c => c.Name);
var dict = childGroups.ToDictionary(cg => cg.Key, cg => cg.Sum(child => child.Value));