When, if ever, is it faster to pass arguments as arguments to a static method rather than have the method be non-static and access the same values via instance members. Assume the method accesses these members in a read-only fashion.
All other things being equal, calling a static method is slightly faster than calling an instance method.
All other things being equal, calling a method with no arguments is slightly faster than calling one with arguments.
Consider:
private Thing _thing;
void DoTheThing()
{
_thing.DoIt();
}
Versus this equivalent code:
private Thing _thing;
// caller's responsibility to pass "_thing"
static void DoTheThing(Thing thing)
{
thing.DoIt();
}
I can't think of a real-world situation where this kind of optimisation would really add any value, but as a thought experiment (for those who like to discuss this kind of thing), is there really a benefit, and if so then how many arguments (of what types etc) tip the balance the other way?
Would any other factors play into the consideration of this? The static method accesses _thing
as a local variable rather than a field, for example.