Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any performance benefits in C# discards?

Tags:

c#

c#-8.0

Consider this code:

var (mult, sum) = MultSum(a, b);

and

var (_, sum) = MultSum(a, b);

Question 1. If I use discard instead of a variable name, does it have performance benefit? eg. by reducing assignment operations.

Question 2. Is there any way to write the MultSum smart enough so it doesn't calculate the discards!?

like image 290
mehrandvd Avatar asked Dec 18 '19 21:12

mehrandvd


People also ask

Is there any benefit in learning C?

By learning C, you will be able to understand and visualise the inner workings of computer systems (like allocation and memory management), their architecture and the overall concepts that drive programming. As a programming language, C also allows you to write more complex and comprehensive programs.

Does C have better performance than C++?

Performance-based on Nature Of Language C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.

Why is C language more efficient?

low level languages - and C is one - allow less advanced constructs and are thus closer to assembler and easier for the compiler to optimize. By more efficient,does it mean the machine code is better,or it takes less time to be 'translated' into machine code?


1 Answers

If I use discard instead of a variable name, does it have performance benefit? eg. by reducing assignment operations.

In your particular case it is unlikely that there would be a benefit in performance. The tuple that is returned is assigned to temporary storage; you've just not given a name to one part of that storage.

Now, if you had an expression that had discards that were entire values, not fragments of a tuple, then the compiler and the jitter can be smart about not allocating any storage on the short-term pool for the result, or re-using existing storage that was already allocated. Note that by "short-term pool" I effectively mean "activation record on the stack" or "registers". This could, in theory, lead to better register allocation or smaller frames (and therefore better locality of reference) and that in turn could save you entire nanoseconds.

Nano-optimizations are generally not worth it; there is almost always a better bang-for-buck performance problem to attack. But if you think it might be relevant for your scenario, measure it and see. That is the only way to know if there is a relevant performance difference. Get out a nano-scale stopwatch, run the code both ways, and see which one is faster.

The benefit you should be attempting to accrue by using discards is the "make my program easier to understand" benefit. Programmers are expensive; optimize for making your code easy for future programmers to read, understand and modify.

Is there any way to write the MultSum smart enough so it doesn't calculate the discards!?

Yes. Write your program in Haskell. Haskell will avoid performing calculations whose results are never used. C# is not such a language.

like image 123
Eric Lippert Avatar answered Oct 18 '22 22:10

Eric Lippert