Skip to content

Instantly share code, notes, and snippets.

@rychlym
Created January 4, 2024 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rychlym/79f98435777e1715a74da6c4e7c94d8b to your computer and use it in GitHub Desktop.
Save rychlym/79f98435777e1715a74da6c4e7c94d8b to your computer and use it in GitHub Desktop.
For loop testings
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