Alex Rivera | Logout

C# performance question

Asked 2012-08-01T16:59:28.593
11

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.

c#
Edit
Report

2 Answers

19

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

answered 2012-08-04T15:01:50.383
8

The answer would most likely be E. In almost all cases, value types will improve performance. First, use of value types in functions create stack space, which is allocated even before the call is made, avoiding object allocation overhead. Second, when creating arrays of value types on the heap, you both avoid the object allocation overhead, and the data tends to be more cache coherent.

It is true that there is potentially memory bandwidth overhead in the copying of value types, but modern memory bandwidth is so huge, the loss is usually grossly overwhelmed by the other savings. In addition, there is effectively no loss when dealing with 64-bit or smaller types.

answered 2012-08-01T17:06:21.983

Your Answer