Feb 13 2011

Knowing what things cost

Category: Intellectual PursuitsJoeGeeky @ 17:01

There is an old saying that knowing what things cost is half the battle, and in this case that is certainly true. Understanding the relative cost for various approaches can be important when you’re looking to get as many nanoseconds out of your process as possible. Here is a summary captured on relatively old equipment, but keep in mind we are looking to understand the relative costs for various choices. If you do a little profiling of your own you may find even more surpises.

Constructing Objects

new value type L1 2.6 ns
new value type L2 4.6 ns
new value type L3 6.4 ns
new value type L4 8.0 ns
new value type L5 22.9 ns
new ref type L1 20.3 ns
new ref type L2 23.9 ns
new ref type L3 27.5 ns
new ref type L4 30.8 ns
new ref type L5 34.4 ns

Note: times will go higher for ref types depending on ctor param arrangement

Arithmetic Operations

Addition 1.0 ns
Subtract 1.0 ns
Multiply 2.7 ns
Divide 35.7 ns
Shift 2.1 ns

Method Calls

static call 6.1 ns
instance call 6.8 ns
instance this call 6.2 ns
Inlined static call 0.2 ns
Inlined instance call 1.0 ns
Inlined instance this call 0.2 ns
virtual call 5.4 ns
this virtual call 5.4 ns
interface call 6.5 ns


cast up 1 0.4 ns
cast down 1 8.8 ns

Field and Property Access

get field 1.0 ns
get property 1.2 ns
set field 1.2 ns
set property 1.2 ns
get virtual property 6.3 ns
set virtual property 6.3 ns


Box int 21.6 ns
Unbox int 3.0 ns


delegate invocation 40.9 ns



trackback Smelser.NET says:

A few principles for writing blazing fast code in .NET

A few principles for writing blazing fast code in .NET

Comments are closed