![]() ![]() Later, we compared the performance of the different approaches and finally came to know that the array index initialization is the fastest approach.Summary : Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update or add a registry key value. In this article, we’ve learned how to add values to a C# array. ArrayIndexInitializer remains the winner of this benchmark. We can also see that the LinqConcat approach performs better than Arra圜opyTo and ListCollection with the increase in size. We can see that the mean increases almost proportionately to the array size. ![]() Now, let’s run the same benchmark for an array size of 10,000 and look for the result: | Method | arraySize | Mean | Error | StdDev | Median | It clearly shows that this approach is not suitable when we want to append a large number of elements to an array. The slowest of all approaches is the LinqAppend approach which is almost ~ 317 times slower than the index initializer approach. Surprisingly, the SetValueMethod approach is ~ 26 times slower than the index initializer approach with a mean of 14,316.8 ns. ![]() The Arra圜opyTo approach is almost close to the ListCollection with a mean of 2,181.7 ns. | LinqAppend | 1000 | 172,341.2 ns | 3,399.26 ns | 8,402.14 ns | 168,804.7 ns |įrom the result, we can clearly see that the ArrayIndexInitializer approach is the fastest with a mean of 542.5 ns for an array size of 1,000.Īnd then, the ListCollection approach is ~ 4 times slower with a mean of 2,151.2 ns. Now, let’s run the benchmark with an array size of 1,000 elements and wait for the result: | Method | arraySize | Mean | Error | StdDev | Median | The other approaches work similarly and you can check the source code if you’re interested. We assign the values to an array in a loop of varying sizes. Let’s look at one of the approaches that we use for benchmark: public static int ArrayIndexInitializer(int arraySize)įor (var index = 0 index < arraySize index++) In this benchmark, we’ll use arrays of different sizes and then compare which approach performs the best in adding values to the array. We’ll use the BenchmarkDotNet library to do the benchmark for us. Now, let’s dive into the interesting part which is performance comparison. In case we want to add an element at the start of the sequence, we need to use Prepend() method. NET/C# content and get paid? > JOIN US! << Wanna join Code Maze Team, help us produce more awesome. In order to resize this array to hold 3 elements, we can simply do: NET/C# content and get paid? > JOIN US! () ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |