Created
January 30, 2020 22:55
-
-
Save joncloud/7ad433b7d67c4c80df7cf4f082d9a904 to your computer and use it in GitHub Desktop.
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.Running; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApp66 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BenchmarkRunner.Run<Tests>(); | |
} | |
} | |
[MemoryDiagnoser] | |
public class Tests | |
{ | |
static List<Guid> _sourceList; | |
static Queue<Guid> _sourceQueue; | |
static List<Guid> _list = new List<Guid>(); | |
static Queue<Guid> _queue = new Queue<Guid>(); | |
[GlobalSetup] | |
public static void GlobalSetup() | |
{ | |
_sourceList = new List<Guid>(); | |
_sourceQueue = new Queue<Guid>(); | |
var i = 10_000; | |
while (--i >= 0) | |
{ | |
var item = Guid.NewGuid(); | |
_sourceList.Add(item); | |
_sourceQueue.Enqueue(item); | |
} | |
} | |
[Benchmark(Baseline = true)] | |
public List<List<Guid>> AsList() | |
{ | |
var target = _list; | |
target.AddRange(_sourceList); | |
var chonk = new List<List<Guid>>(); | |
do | |
{ | |
var list = target.Take(900).ToList(); | |
chonk.Add(list); | |
target.RemoveRange(0, list.Count); | |
} while (target.Any()); | |
return chonk; | |
} | |
[Benchmark] | |
public List<List<Guid>> AsQueue() | |
{ | |
var target = _queue; | |
for (var i = 0; i < _sourceList.Count; i++) | |
{ | |
target.Enqueue(_sourceList[i]); | |
} | |
var chonk = new List<List<Guid>>(); | |
do | |
{ | |
var list = new List<Guid>(900); | |
while (list.Count < 900 && target.TryDequeue(out var item)) | |
{ | |
list.Add(item); | |
} | |
chonk.Add(list); | |
} while (target.Any()); | |
return chonk; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment