Reputation: 12457
From some researching, I know the static method below can get JIT inlined. But can the non-static ones get inlined as well?
If the code is really time-critical, should I bother valuing this kind of static methods over non-static ones?
public struct PixelData
{
public readonly uint raw;
public PixelData(uint raw)
{
this.raw = raw;
}
//JIT inlines this
static bool HasLineOfSight(PixelData p)
{
return (p.raw & 0x8000) != 0;
}
//But what about this?
int GetWeight()
{
return (int)(raw & 0x3FFF);
}
}
Upvotes: 0
Views: 223
Reputation: 28097
You could apply [MethodImpl(MethodImplOptions.AggressiveInlining)]
to the method to try and force inlining:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
int GetWeight()
{
return (int)(raw & 0x3FFF);
}
However, from this programmer.se answer you can see that inlining vs. not actually makes very little difference:
Results
struct get property : 0.3097832 seconds struct inline get property : 0.3079076 seconds struct method call with params : 1.0925033 seconds struct inline method call with params : 1.0930666 seconds struct method call without params : 1.5211852 seconds struct intline method call without params : 1.2235001 seconds
You're best letting the compiler do its thing until you find that you're seeing some real measurable performance issues.
Upvotes: 3
Reputation: 330
MSDN: Static Classes and Static Class Members
A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference between the two is not significant.
Upvotes: 1