-
-
Notifications
You must be signed in to change notification settings - Fork 970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fast collection initialization for benchmark #2624
Comments
The initialization logic should not be part of the benchmark: https://github.com/dotnet/performance/blob/main/docs/microbenchmark-design-guidelines.md#setup (I very much encourage you to read the whole doc)
Over here you can see how we have implemented collection benchmarks on the .NET Team: https://github.com/dotnet/performance/tree/main/src/benchmarks/micro/libraries/System.Collections |
@adamsitnik Maybe I was not clear enough. Of course the initialization logic is not part of the benchmark. However it is done in Methods decorated as
I now that benchmarks. However they are not much of a help. At no place they have the need to benchmark Add, Remove, Lookup performance of a collection containing already 100_000_000 items. And to prepare the prefilled collections is the thing that is time consuming. I do not have a problem with the benchmarks itself or the structure. That I have figured out nicely. |
I know this is not a problem of BenchmarkDotNet. But maybe some of you folks did have a similar problem and already have a solution at hand. I also asked on stackoverflow but there are only some cheap asses at the moment and the only thing they can respond is "please provide a minimal reproducible example showing your code"
Well here is my problem
For a benchmark I need to create a set of different collections to measure their performance for certain specific tasks.
My problem is that initialization of this collections slows down the whole benchmarking.The collections are initialized 100 and 1000 of times during the benchmark so every second saved counts.
I optimized my code for SortedList and ImmutableSortedDictionary (see code example below).
My problem is that while I managed to initialize SortedList and ImmutableSortedDictionary in a few Seconds. The SortedDictionary takes several minutes.
What I need is a way to initalize SortedDictionary as fast as possible. Id does not need to be a DeepCopy since Keys and Values will not be modified. In fact I would prefer a ShallowCopy of the Data inside the Collection to save some memory during the Benchmark. The only thing that is important that the collections do not interfere with each other. For example, I tried to use the ShallowClone extension from DeepCloner nuget package. This extension manages to clone the SortedDictionary in Seconds but when I add an Item in one of this Dictionaries all other Dictionaries do get modified too which is an unwanted side effect since they share the same SortedTree object.
And using the constructor of SortedDictonary the creation of 100 SortedDictionary with 1000000 entries take ~9 minutes.
repeatCount is the number of SortedLists to create, for this specific test 100.
PrefillArray holds 1000000 random generated items.
I am trying to bring that down to ~2 seconds so that my benchmark set can finish in an acceptable timeframe.
My fastest found solution at the moment is (for a complede code example look to the bottom)
Minimal reproducible example:
Any ideas?
A working and updated example if I found a faster solution can be viewed here: https://dotnetfiddle.net/1Enx1q
However I did have to lower some values because of memory limitations.
The text was updated successfully, but these errors were encountered: