2022-06-21 08:09:38 +02:00
|
|
|
using System.Collections.Generic;
|
2018-12-05 12:57:23 +01:00
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Tests.Benchmarks;
|
|
|
|
|
|
|
|
|
|
[MemoryDiagnoser]
|
|
|
|
|
public class EnumeratorBenchmarks
|
2018-12-05 12:57:23 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark(Baseline = true)]
|
|
|
|
|
public void WithArray()
|
2018-12-05 12:57:23 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
foreach (var t in EnumerateOneWithArray(1))
|
2018-12-05 12:57:23 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
;
|
2018-12-05 12:57:23 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2018-12-05 12:57:23 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Benchmark]
|
|
|
|
|
public void WithYield()
|
|
|
|
|
{
|
|
|
|
|
foreach (var t in EnumerateOneWithYield(1))
|
2018-12-05 12:57:23 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
;
|
2018-12-05 12:57:23 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2018-12-05 12:57:23 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private IEnumerable<T> EnumerateOneWithArray<T>(T o) => new[] { o };
|
2018-12-05 12:57:23 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private IEnumerable<T> EnumerateOneWithYield<T>(T o)
|
|
|
|
|
{
|
|
|
|
|
yield return o;
|
2018-12-05 12:57:23 +01:00
|
|
|
}
|
|
|
|
|
}
|