Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save admir-live/000aac58c9e7d4bffdad83a9829a60d6 to your computer and use it in GitHub Desktop.

Select an option

Save admir-live/000aac58c9e7d4bffdad83a9829a60d6 to your computer and use it in GitHub Desktop.
π—¦π˜‚π—Ί π— π—²π˜π—΅π—Όπ—±
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BenchmarkExample
{
public class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<BasketBenchmarks>();
}
}
public class BasketItem
{
public decimal Price { get; set; }
}
public class BasketBenchmarks
{
private List<BasketItem> _baskets;
[GlobalSetup]
public void Setup()
{
_baskets = new List<BasketItem>();
for (int i = 0; i < 1000; i++)
{
_baskets.Add(new BasketItem { Price = i });
}
}
[Benchmark]
public decimal ForeachSum()
{
decimal total = 0;
foreach (var item in _baskets)
{
total += item.Price;
}
return total;
}
[Benchmark]
public decimal LINQSum()
{
return _baskets.Sum(current => current.Price);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment