Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# performance question

Tags:

c#

I just took an IKM C# test. One of the questions was:

Which of the folowing IMPROVE the performance of a C# program?

  • A. Use boxing
  • B. Use unboxing
  • C. Do not use constants
  • D. Use empty destructors
  • E. Use value type instead of reference type

In the end I skipped the question, the only possible answer I can see is E. In some situations value types could provide better performance (for small types: no dereferencing required and not on the managed heap [assuming not a member of a reference type]), but that's certainly not always the case.

like image 561
MrNick Avatar asked Aug 01 '12 16:08

MrNick


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

Focusing a bit on the wrong answers:

A boxing conversion occurs when a value type value is converted to a reference type value, an object. It involves allocating memory from the garbage collected heap, creating an object header that identifies the object as being of the type of the value type and copying the value type value bits into the object. This is the conversion that creates the type system illusion that a value type derives from System.ValueType and System.Object. Boxing conversions were heavily used in .NET 1.x programs since the only collection types it supported where the classes in System.Collections, collections whose elements are Object. .NET Generics added in 2.0 made these classes instantly obsolete since that allowed creating the classes in System.Collections.Generic. Which can store a value without having to box it. So no.

Unboxing is the opposite conversion, going from a boxed object value back to the value type value. Not quite as expensive as boxing, it only involves checking if the type of the boxed object is of the expected type and copying the value type value bits. It requires a cast in C# and is prone to throwing exceptions when the boxed value type does not match. Same no as the previous one.

Identifiers marked with the const keyword are literal values that are directly compiled into the IL generated by the compiler. The alternative is the readonly keyword. Which requires a memory access to load the value and is thus always slower. A const identifier should always be private or internal, public constants have a knack for breaking a program when you deploy a bug fix that alters the value but don't recompile the assemblies that use the constant. Those assemblies will still use the old constant value since it was compiled into their code. A problem that can't happen with readonly values. So no.

A destructor (aka finalizer) considerably increases the cost of an object. The garbage collector ensures that a finalizer is called when an object is garbage collected. But to do so, it has to keep track of the object separately, such an object is put on the finalizer queue, waiting for the finalizer thread to get around to executing the finalizer. The object doesn't actually truly get destroyed until the next GC pass. You almost always have the class for such an object implement IDisposable so a program can invoke the duties of the finalizer early and not burden the runtime with doing it automatically. You call GC.SuppressFinalize() in your Dispose() method. Nothing worse than a finalizer that doesn't do anything, so no.

Value types exist in .NET for the express reason that they can be so much more efficient than reference types. Their values take a lot less memory than an reference type object and can be stored in CPU registers and the CPU stack, memory locations that are highly optimized in processor designs. They burden the language design since abstracting them as objects is a leaky abstraction that swallows cpu cycles undetectably, particularly a struct is a difficult type with a knack for breaking programs when you try to mutate them. But one that's important to avoid the kind of perf hit that super pure languages like Smalltalk suffer from. A pioneer OOP language where every value is an object and which influenced a large number of subsequent OOP languages. But rarely actually got used anywhere due to its poor performance without a clear path for the hardware engineers to make it as fast as languages that don't abstract the processor design away. Like C#. So that makes it E.

like image 66
Hans Passant Avatar answered Sep 22 '22 18:09

Hans Passant