Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created February 10, 2025 14:46
Show Gist options
  • Save davepcallan/959bda54bcc92f769e6bb8b933717e5a to your computer and use it in GitHub Desktop.
Save davepcallan/959bda54bcc92f769e6bb8b933717e5a to your computer and use it in GitHub Desktop.
Benchmark for Skip-Take, Take-Range and GetRange when working with List<T>
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using System.Collections.Generic;
using System.Linq;
namespace Benchmarks
{
[Config(typeof(Config))]
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)]
[SimpleJob(RuntimeMoniker.Net90)]
[MemoryDiagnoser]
[ReturnValueValidator(failOnError: true)]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.SlowestToFastest)]
public class GetRangeBenchmark
{
private List<string> _userIds;
[GlobalSetup]
public void Setup()
{
_userIds = Enumerable.Range(1, 1000).Select(i => $"User{i}").ToList();
}
[Benchmark(Baseline = true)]
public List<string> SkipAndTake()
{
return _userIds.Skip(200).Take(200).ToList();
}
[Benchmark]
public List<string> Take()
{
return _userIds.Take(200..400).ToList();
}
[Benchmark]
public List<string> GetRangeMethod()
{
return _userIds.GetRange(200, 200);
}
private class Config : ManualConfig
{
public Config()
{
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment