Last active
April 23, 2024 16:35
-
-
Save sa-es-ir/a29b49357e9127493b3ca3138b112b01 to your computer and use it in GitHub Desktop.
A Comparison about C# For loops
This file contains 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.Jobs; | |
using System.Runtime.InteropServices; | |
namespace ForLoopsComparison; | |
[MemoryDiagnoser(false)] | |
[SimpleJob(RuntimeMoniker.Net60)] | |
[SimpleJob(RuntimeMoniker.Net80)] | |
[SimpleJob(RuntimeMoniker.Net90)] | |
public class AllForLoopBenchmark | |
{ | |
[Params(100_000)] | |
public int ItemCount { get; set; } | |
public List<int> Items; | |
[GlobalSetup] | |
public void GlobalSetup() | |
{ | |
Items = Enumerable.Range(0, ItemCount).ToList(); | |
} | |
[Benchmark] | |
public void For() | |
{ | |
for (int i = 0; i < ItemCount; i++) | |
{ | |
DoSomeThing(Items[i]); | |
} | |
} | |
[Benchmark] | |
public void ForEach() | |
{ | |
foreach (var item in Items) | |
{ | |
DoSomeThing(item); | |
} | |
} | |
[Benchmark] | |
public void ForEach_Linq() | |
{ | |
Items.ForEach(DoSomeThing); | |
} | |
[Benchmark] | |
public void ForEach_Parallel() | |
{ | |
Parallel.ForEach(Items, DoSomeThing); | |
} | |
[Benchmark] | |
public void ForEach_LinqParallel() | |
{ | |
Items.AsParallel().ForAll(DoSomeThing); | |
} | |
[Benchmark] | |
public void For_Span() | |
{ | |
var span = CollectionsMarshal.AsSpan(Items); | |
for (int i = 0; i < span.Length; i++) | |
{ | |
DoSomeThing(span[i]); | |
} | |
} | |
[Benchmark] | |
public void ForEach_Span() | |
{ | |
foreach (var item in CollectionsMarshal.AsSpan(Items)) | |
{ | |
DoSomeThing(item); | |
} | |
} | |
private void DoSomeThing(int i) | |
{ | |
_ = i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment