Home > Software engineering >  Is it faster to get a property or to pass a value to a method
Is it faster to get a property or to pass a value to a method

Time:12-20

I have a static class with properties to store user's inputs:

public static class UserData
{
    public static double UserInput1 { get; set; }
}

And I have nested methods that need the user's inputs

public static double Foo()
{
    [...]
    var input1 = UserData.UserInput1;
    var bar = Bar();
    [...]
}

private static double Bar()
{
    var input1 = UserData.UserInput1;
    [...]
}

The positive thing is that I do not have to pass all user inputs to Foo(), then to Bar() (and to further nested methods within Bar()). The negative thing is that I have to get UserData.UserInput1 and other user inputs very often. I could change the code to get the user inputs only once:

public static double Foo()
{
    [...]
    var input1 = UserData.UserInput1;
    var bar = Bar(input1);
    [...]
}

private static double Bar(double input1)
{
    [...]
}

Which one is faster?

CodePudding user response:

Which one is faster?

Using static mutable state in this way will be way slower in the long run. Because you will spend a bunch of time trying to find and fix bugs. This time could be better spent doing things that will actually help performance, like profiling and optimizing code.

Try to make method that compute anything take the required input as parameters. Try to make input fields properties of the associated UI class. This should help keep the code simple and understandable.

Accessing a static property will be translated to a indirect memory access. Passing a parameter to a method might be free if the parameter is already in a register, or might involve a bit more work if it needs to be loaded, moved or passed on the stack. But we are talking about single digit cycles here, optimization on this level should only be done in super tight loops that are run many millions of times each second, and then you should typically ensure that all methods can be inlined, side stepping the problem.

CodePudding user response:

The second one is the faster than the first one. Because you avoid to obtain the static property from UserData.

It's not a big goal works with static when we talk about performance cost due to the need to perform a lookup in the symbol table and track shared memory. By passing input values as parameters, this is avoided and slightly better performance is achieved.

But both options are ok. It's more important to focus on code readability and maintainability rather than performance unless you are working on a critical performance issue.

CodePudding user response:

If you're worried about such micro-optimizations (which you generally wouldn't need), consider using inlining.

[MethodImpl(MethodImplOptions.AggressiveInlining)]

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.methodimploptions?view=net-7.0

PS: using one over the other, or using AggressiveInlining, will not save you anywhere close to the 1ms you are hoping for, under non-extreme/farfetched scenarios.

  • Related