Surprisingly different performance of simple C# program

I think the reason is CPU instruction pipelining. your slow equation depends on vs, that means vs must be calculated first, then vl is calculated.

but in your fast equation, more instructions can be pipelined as vs and vl can be calculated at same time because they don't depend on each other.

Please don't confuse this with multi threading. Instruction pipelining is some thing implemented at very low hardware level and tries to exploit as many CPU modules as possible at the same time to achieve maximum instruction throughput.


You calculations are not equal

double vL = (2 * ms * us - uL * (ms - mL)) / (ms + mL); //fast
double vL = uL + ms * (us - vs) / mL; //slow

Example: I miss vs in the fast version

I expect your while loop doing more iterations because of this?

Tags:

C#

Performance