Created
June 25, 2024 19:40
-
-
Save admir-live/000aac58c9e7d4bffdad83a9829a60d6 to your computer and use it in GitHub Desktop.
π¦ππΊ π π²ππ΅πΌπ±
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
| 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