Created
January 13, 2023 23:22
-
-
Save Henr1k80/d6f0404b48a547ca71e05e78b87061e6 to your computer and use it in GitHub Desktop.
Benchmark of cast to IEnumerable<int> vs call of the extension method .AsEnumerable()
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 BenchmarkDotNet.Running; | |
using Cysharp.Text; | |
BenchmarkRunner.Run<CastVsAsEnumerable>(); | |
[MemoryDiagnoser] | |
[SimpleJob(RuntimeMoniker.Net60)] | |
public class CastVsAsEnumerable | |
{ | |
private const string Separator = "_,_"; | |
private static readonly Random Random = new(60); | |
[Params(0, 1, 100)] | |
public ushort Size { get; set; } | |
private readonly HashSet<int> _hashSet = new (); | |
[GlobalSetup] | |
public void GlobalSetup() | |
{ | |
for (var i = 0; i < Size; i++) | |
_hashSet.Add(Random.Next(int.MinValue, int.MaxValue)); | |
} | |
[Benchmark] | |
public string Cast() | |
{ | |
return ZString.Join(Separator, (IEnumerable<int>)_hashSet); | |
} | |
[Benchmark] | |
public string AsEnumerable() | |
{ | |
return ZString.Join(Separator,_hashSet.AsEnumerable()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment