BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

[Question] Benchmarking Collection operations

Open darthkurak opened this issue 3 years ago • 0 comments

Hi. I try to benchmark some collection operation like Add on List. For example, I want to benchmark List<T>.Add on different size of pre-populated collection to show difference in Capacity growing. Something like this:

public class CollectionTests
{
    private static List<Student> _initialList = AutoFaker.Generate<Student>(131071);
    private List<Student> _listA; //adding to this still fit Capacity
    private List<Student> _listB; //adding to this should grow Capacity
    
    public CollectionTests()
    {
        _listA = new List<Student>(_initialList);
        _listB = new List<Student>(_initialList);
        _listB.Add(AutoFaker.Generate<Student>());
    }

    [Benchmark]
    public void List_A()
    {
        _listA.Add(AutoFaker.Generate<Student>());
    }

    [Benchmark]
    public void List_B()
    {
        _listA.Add(AutoFaker.Generate<Student>());
    }
}

what is the best approach to this? To have pre-defined collections and reuse them between iterations, but do not count collection preparation to execution time? This static approach and using GlobalSetup as well, do not give me correct results.

darthkurak avatar Nov 02 '21 21:11 darthkurak