Created
January 4, 2024 21:21
-
-
Save rychlym/79f98435777e1715a74da6c4e7c94d8b to your computer and use it in GitHub Desktop.
For loop testings
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 System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
namespace MyTests; | |
public class ForEachTests | |
{ | |
private List<int> items = Enumerable.Range(1, 100).ToList(); | |
public void ForEach() | |
{ | |
foreach (int item in items) | |
{ | |
} | |
} | |
public void For() | |
{ | |
for (var i = 0; i < items.Count; i++) | |
{ | |
var item = items[i]; | |
} | |
} | |
public void ForEachSpan() | |
{ | |
var asSpan = CollectionsMarshal.AsSpan(items); | |
foreach (int item in asSpan) | |
{ | |
} | |
} | |
public void ForSpan() | |
{ | |
var asSpan = CollectionsMarshal.AsSpan(items); | |
for (var i = 0; i < asSpan.Length; i++) | |
{ | |
var item = asSpan[i]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment