This article is transcribed from Bartosz Adamczewskiâs video on LevelUpâs Youtube channel about 5 (Extreme) Performance Tips in C#.
This transcrip...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
This article is copied wholesale from my youtube video: youtube.com/watch?v=Tb2Fx9qku_o
If you're going to copy all of the things 1to1, then at least you should credit the author.
Overall a good article, but the example code has a lot of bugs. So it is hard to know if what is shown was used for your performance tests. If it was then your speed improvements may have more to do with optimizations for invariants. For instance in #3 you add to âcounterâ, but return the sum of counterA and counterB which are both zero. Similar issue in #4, and it also uses the wrong value for the B portion (youâre using p[0] for both A and B sums.
Found how to eliminate multiplication. It makes the code just a little bit faster compared to SumOdd_BranchFree but this technique performs worse than multiplication methods with parallel execution.
The code is the following:
Here's one more option to consider. It's twice as fast as the best one above and uses the bitwise step in actual parallel processing. It probably could have further optimizations from above incorporated into it also. With this you'd call ParallelLoop(array, numberOfTasksYouWantForYourMachine).
Also, I had never thought of bit checking before and have always used mod. I will certainly use this in the future. Nice article!
private static long AddViaGap(long[] array, int gap, int offset) {
long counter = 0;
for (int i = offset; i < array.Length; i += gap) {
var element = array[i];
var odd = element & 1;
counter += (odd * element);
}
return counter;
}
private static long ParallelLoop(long[] array, int taskCount) {
var tasks = new Task[taskCount];
for (var counter = 0; counter < taskCount; counter++) {
var currentCounter = counter;
tasks[counter] = Task.Run(() => AddViaGap(array, taskCount, currentCounter));
}
Task.WaitAll(tasks);
return tasks.Sum(o => o.Result);
}
Good tips, thanks. Not strong on C# myself but surely you've introduced a bug at stage 3, e.g. if your array has an odd length? I.e. elementB won't exist.
You could get rid of the multiplication by defining a array with length 2.
First entry at Index 0 is 0
Second entry at index 1 is -1
Instead of multiplying value by 1 or 0, you could simply do a Bitwise AND Operation with the value at Index 1 or 0.
great article! thanks for sharing!
đđđđđ
Nice article.
âââââ
Link to the original video
youtu.be/Tb2Fx9qku_o