5 (Extreme) Performance Tips in C#

preview_player
Показать описание
In this video, I'm going to show you 5 performance tips (or tricks) that you can apply in order to make your C# code run faster.

Everest Photo by Mário Simoes

#csharp #dotnet #dotnetcore #performance #internals #optimizations
Рекомендации по теме
Комментарии
Автор

Do you know any other tips that wasn't mention in this video?

@Gilad Freidkin has provided a couple interesting ones as well.

LevelUppp
Автор

OK so the trick is to have a longer method name!

psychotrout
Автор

There's a potentially faster version of your no-multiplication bit hacks:

// For n-bit integers, use a shift of (n-1)
counter += value & (value & 1) << 31 >> 31;

To explain it, I'll use 8-bit integers for brevity:
1. (value & 1): is the value odd?
2. << 31: move the pseudo-boolean value to the sign bit
3. >> 31: perform a sign-extending(!) shift to the right, essentially creating a move mask
4. value &: use the move mask to either zero out or keep the value

It'll look something like this:
Value: 5
1. & 1) = 1
2. 1 << 7 =
3. >> 7 =
4. & = 5

Value: 6
1. & 1) = 0
2. 0 << 7 = 0
3. 0 >> 7 = 0
4. 6 & 0 = 0

This method eliminates not only the multiplication, but also the subtraction. Would be interested to see if it's actually faster, though

Zooiest
Автор

Good video thanks. By the way your loop will throw an error if your array has odd length. Therefore instead of write i < array.length ; i +=2; you should write i < array.length - 1; i += 2

TheHackhell
Автор

The most extreme performance tip. Shut down your computer and go climb Mount Everest.

SmartK
Автор

I always see "boolAsInt * something" where "-boolAsInt & something" is twice as fast. 0 or 1 times something is the same as -0 = 0x0000_0000 or -1 = 0xFFFF_FFFF AND something.
Code size and register dependencies increase (the latter doesn't really count when it replaces an operation that takes long, like multiplying ints at ~ 4 clock ticks, which also has low throughput) so that might matter. Your bit hack is slower than a multiply because bit shifting by a non compile time constant is pretty slow (up to 7 clock ticks) and it only works with ONE particular register with X86, being CL (=> no ILP).

Mortuus
Автор

Great performance tips.
How about :
1^2 =1
2^2 =1+3
3^2 = 1+3+5
...
Sorry if I formulate this wrong:
Sum of x odd= (x//2 + x%2)^2

javiermunoz
Автор

@LevelUp would love to see those simd instructions and other tricks in a new video :)
Thanks for showing these tricks!

jetersen
Автор

for (int i = 0; i < array.Length; i += 2) sum += array[i];

HikingUtah
Автор

Wow thank you so much, all solid performance tips, cheers

mumk
Автор

In fact, we rarely use Array in real world. Furthermore we can use multitasking for CPU-bound tasks or asynchronous for I/O-bound tasks to improve performance

JLPT-AIsensei
Автор

You should do it in parallel, where your total number of partitions is the same as the number of channels supported by your CPU (4 or 8 most common I believe), but not greater than the number of CPUs available.

rmcgraw
Автор

Sorry for this very noob question. @6:33 if the values of oddA and oddB can both only be 1 or 0, then why do our counters need to be added by the strange values (oddA * elementA) and (oddB * elementB)? If we're just counting how many odd numbers are in the array couldn't we just write counterA += elementA & 1; and counterB += elementB & 1; ? I don't use bitwise logic in the code that I write and I also have never considered ports, registers or memory addresses, so please understand that I'm swimming in water that's over my head here, and thank you for the very interesting video. PS~ I _LOVE_ that parallelism trick and I know of at least one spot in my code base where I think I can make use of it, thanks!

matthewexline
Автор

Perhaps branch-free is my biggest takeaway

DoorThief
Автор

what if instead of multiplying you fill up the whole integer with the first bit from the & 1 result and & that with p[x]

gerakore
Автор

Don't most compilers which optimize already do most all of this stuff (like unwrapping for loops)?

JJCUBER
Автор

thanks for the video,
-Isn't it a waste of time to use var type even though you know the type of the variable? (it should waste time for finding type)
-what will happen if your array has 7 elements, your parallism in loop will be out of the array is it?

caglarcansarikaya
Автор

How about conditional move, by ? : ternary operator?

stevejin
Автор

Doesn't the compiler do most of this when you run in release mode?

TheMusterionOfRock
Автор

Where do you learn such things ? What was your learning path on thing topic?

openroomxyz