diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs index 8758cc2..f9451d6 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs @@ -102,6 +102,118 @@ namespace Recyclable.Collections.Benchmarks } } + [Benchmark] + public void RecyclableList_GetItem_AsSpan() + { + var data = _testRecyclableList; + var dataCount = TestObjectCount; + int blockSize = data.BlockSize; + int nextItemBlockIndex = data.NextItemBlockIndex; + + var memoryBlocks = new Span(data.MemoryBlocks, 0, data.LastTakenBlockIndex + 1); + for (var blockIndex = 0; blockIndex < nextItemBlockIndex; blockIndex++) + { + var memoryBlock = new Span(memoryBlocks[blockIndex], 0, blockSize); + for (var itemIndex = 0; itemIndex < blockSize; itemIndex++) + { + DoNothing(memoryBlock[itemIndex]); + } + } + + if (nextItemBlockIndex == data.LastTakenBlockIndex) + { + + var memoryBlock = new Span(memoryBlocks[nextItemBlockIndex], 0, data.NextItemIndex); + for (var itemIndex = 0; itemIndex < memoryBlock.Length; itemIndex++) + { + DoNothing(memoryBlock[itemIndex]); + } + } + } + + [Benchmark] + public void RecyclableList_GetItem_AsArray() + { + var data = _testRecyclableList; + var dataCount = TestObjectCount; + int blockSize = data.BlockSize; + int nextItemBlockIndex = data.NextItemBlockIndex; + + var memoryBlocks = data.MemoryBlocks; + for (var blockIndex = 0; blockIndex < nextItemBlockIndex; blockIndex++) + { + var memoryBlock = memoryBlocks[blockIndex]; + for (var itemIndex = 0; itemIndex < blockSize; itemIndex++) + { + DoNothing(memoryBlock[itemIndex]); + } + } + + if (nextItemBlockIndex == data.LastTakenBlockIndex) + { + var memoryBlock = memoryBlocks[nextItemBlockIndex]; + blockSize = data.NextItemIndex; + for (var itemIndex = 0; itemIndex < blockSize; itemIndex++) + { + DoNothing(memoryBlock[itemIndex]); + } + } + } + + [Benchmark] + public void RecyclableList_GetItem_AsForEach() + { + var data = _testRecyclableList; + var dataCount = TestObjectCount; + + foreach (var item in data) + { + DoNothing(item); + } + } + + [Benchmark] + public void RecyclableList_GetItem_AsWhile() + { + var data = _testRecyclableList; + int dataCount = TestObjectCount; + if (dataCount == 0) + { + return; + } + + int blockSize = data.BlockSize; + int nextItemBlockIndex = data.NextItemBlockIndex; + int itemIndex = 0; + int blockIndex = 0; + var memoryBlocks = data.MemoryBlocks; + var memoryBlock = memoryBlocks[0]; + while (blockIndex < nextItemBlockIndex) + { + DoNothing(memoryBlock[itemIndex]); + if (itemIndex + 1 < blockSize) + { + itemIndex++; + } + else + { + blockIndex++; + memoryBlock = memoryBlocks[blockIndex]; + itemIndex = 0; + } + } + + if (nextItemBlockIndex == data.LastTakenBlockIndex) + { + memoryBlock = memoryBlocks[nextItemBlockIndex]; + blockSize = data.NextItemIndex; + while (itemIndex < blockSize) + { + DoNothing(memoryBlock[itemIndex++]); + } + } + } + //[Benchmark] public void RecyclableList_SetItem() {