views:

797

answers:

4

I want to know this info to reduce my code size so I will not waste my time optimize things that will be done by compiler or JIT.

for example:

if we assume the compiler inline the call to the get function of a property so I do not have to save the return value in a local variable to avoid function call.

I want to recommend a good reference that describes what is going on?

+9  A: 

If you are worried about performance, run a profiler. Then change code. Chances are that you will never in a million years guess 100% correctly where the time is going. You could be changing the 0.02% timing, and leaving the method that contributes 62% of the burden. You could also be making it worse. Without a profiler and evidence, you are blind.


You can't assume that the JIT will inline a property getter. There are many reasons it may or may not do so; size of the method body, virtual, value vs reference type, architecture, debugger attached, etc.

"Hoisting" still has a place, and can still achieve savings if the code is called repeatedly in a tight loop; for example:

var count = list.Count;
for(int i = 0 ; i < count ; i++) {...}

(forget the for vs foreach debate fr the above - this is an orthogonal discussion). In the above, the "hoist" will help performance. But just to be really confusing - with arrays, it is the opposite, and it is more efficient to not hoist it:

for(int i = 0 ; i < arr.Length ; i++) {...}

The JIT recognises this and removes the bounds check (as arrays are fixed size).

Marc Gravell
I didn't know about that last one! Nice to know, thanks!
Groo
Thanks for information, but I am asking about a good reference for this information
Ahmed Said
My point is that *no* reference will really help you with this. A **profiler** will.
Marc Gravell
plusitty plus plus. the reference will help you *after* the profiler shows you what's taking time. No reference can cover the broad spectrum of possible combinations of things that exist in the real world.Note that many profilers don't allow (or must be told to allow) inlining of functions so that can skew your profiling information.For most people I doubt that this is significant. If you think inlining is the big deal you shoudl really know to be using a profiler...
ShuggyCoUk
+1  A: 

This looks like a kind of micro optimization which you shouldn't be looking at. If I'm not mistaken it depends on the architecture and version of the CLR which kind of optimization is applied.

If your method is called that much, and you really want it to inline, you can inline it yourself at the cost of spaghetti code.

I would recommend analyzing your algorithm, to inline a method will not save magnitudes of speed, while a better algorithm can make your running time decrease from hours to seconds.

Davy Landman
+7  A: 

You may want to take a look at these articles:

JIT Optimizations - (Sasha Goldshtein - CodeProject)
Jit Optimizations: Inlining I (David Notario)
Jit Optimizations: Inlining II (David Notario)

To be honest you shouldn't be worrying too much about this level of micro-detail. Let the compiler/JIT'er worry about this for you, it's better at it than you are in almost all cases. Don't get hung up on Premature Optimisation. Focus on getting your code working, then worry about optimisations later on if (a) it doesn't run fast enough, (b) you have 'size' issues.

HTH
Kev

Kev
A: 

The most powerful optimization performed by a JIT is typically inlining. A JIT can even inline hundreds of functions deep (I heard this figure for JikesRVM). They will even inline things that aren't always possible to inline, and back it out later if they need to (called dynamic deoptimization).

A nice overview is http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html.

For your specific question, I would say probably, if the function call in question is hot.

Paul Biggar