Nugget 3: Performance Testing with BenchmarkDotNet


Collections in our data model are primarily represented by arrays. This Nugget intends to explore the performance of adding new items to a child collection of a complex object.

A simple project representing the NHL was created for this test. The solution explorer looks like this:





The Array Extension extension method is used to hide how a new item is added to the array. Note that using this extension affected the performance of adding new Players to the PoolPlayer array collection. Is this significant? It is only telling me that it needs careful attention. For this reason, the method using the extension was used as a baseline. The other two methods were benchmarked against this method.

Reference 1 in this Nugget explains how the run is done. BenchmarkDotNet even gives us an estimated running time.


Conclusion

From the run, it seems that using a List to add new items to the collection is more efficient. Although all methods execute similarly, based on the Ratio using the list is faster. Using the list to add new items also performed much more efficiently based on how the memory was managed and how much memory was allocated to this run compared to the other two methods.

It would have been beneficial to compare performance between frameworks and compare how these methods perform in net 4.7.2 vs Net6.0. Note that the above tests were executed using Net6.0, which is .Net Core.

Although this is a very small test, its results indicate that I need to be careful when manipulating arrays and that it might be better to convert the array to a list, manipulate the list, delegating to the list implementation for the best way to execute mundane operations and once my business logic is completed convert the list to array and assign it to the object collection.

References:

Comments

Popular posts from this blog

Nugget 9: Building a JobBank App using Blazor. A Nugget with a Story.

Nugget 14: Connection to MongoDB Atlas - SSL Alert 80

Nugget 11: Adding Filtering to a Blazor QuickGrid