4

I have a class that has a public property. In a single function I refer to this property around 30-40 times.

  this.MyProp; 

Would it be better to define a local variable in the function?,

 string myProp = this.MyProp;

After doing this - in the function I've shortened the lookup chains... so I only have to refer to myProp, rather than this.MyProp.

In JavaScript this shortening of lookups really improves performance. Would it be better / worse in C#? Because obviously, I've also needed to create another local string variable.

1
  • I'd love to know this myself. In .NET 1.1, a common recommendation was to assign the property to a local first to avoid repeated get method executions. I don't know if more recent versions of the C# compiler are smarter about this. Sep 12, 2009 at 18:19

6 Answers 6

11

From a pure performance perspective, it depends on what the property is doing. If the MyProp getter just returns a private field, the overhead is totally inconsequential. In fact, unless you're obviously doing something significant like some huge enumeration or calling into a database, it's of no concern. Worrying about this is micro-micro optimization.

It's important to note the purpose of properties though, which is to force access to a value through an encapsulated routine to ensure consistency. If you try to bypass that, you're exposing your code to greater risk for bugs.

From @DavidAndres comments:

According to convention, properties should typically not do anything substantial in the getter or setter. So I would say it's of no concern if you simply verify, via reflector or documentation or looking at your own code, that the property behaves well. Handling the situations where the value clearly does need to be localized should be an edge case, after it's been verified that it is necessary.

Edit: To be clear, I am not recommending you generally avoid local values for any performance reason. As @Guffa notes the impact is trivial either way. I am pointing out that properties are usually properties for a reason, and access to the value should go through the property by default.

8
  • I also have another more complex scenario , where I have 3 complex objects, stored as properties in 1 object.... Would it be a good idea to localize these more complex objects?
    – JL.
    Sep 12, 2009 at 18:21
  • @JL no. Just use the properties.
    – Rex M
    Sep 12, 2009 at 18:22
  • @Rex: I think it is of no concern if you're working with your own code, with full awareness of what properties are duoing "under-the-hood." If you're integrating third-party code into your libraries, then reducing property accesses may be a micro-optimization as opposed to a micro-micro-optimization, as you put it. Sep 12, 2009 at 18:25
  • 1
    @DavidAndres according to convention, properties should typically not do anything substantial in the getter or setter. So I would say it's of no concern if you simply verify, via reflector or documentation or looking at your own code, that the property behaves well.
    – Rex M
    Sep 12, 2009 at 18:30
  • 2
    @Akash performance only counts when there is a problem. If we haven't profiled and identified the property as a bottleneck, there is no problem yet.
    – Rex M
    Sep 12, 2009 at 18:48
1

Don't be afraid to declare local variables, it's almost free. On a 32 bit system the string variable will use four bytes of stack space. The stack frame where the local variables are allocated is always created, so it takes no extra execution time to allocate the variable.

However, whether you should use a local variable or not should rather be based on what's more correct in the situation. Unless you have a loop where you use the value something like a thousand times or more, the performance difference will hardly even be measurable.

(This is of course based on the assumption that the propery is properly implemented so that it isn't expensive to read.)

0

You normally don't need "this." to refer to a property or field in the same class. I only use it if there's a naming conflict. Microsoft's naming guidelines avoid the conflicts, so I don't use it. :)

As an aside, if you have a single method that refers to a property 30-40 times, it's likely that you have a Long Method smell.

EDIT: I didn't intend to start a religious war over the use of "this." (See Do you prefix your instance variable with ‘this’ in java ?) In our company we prefix fields with underscores (which is another hot topic); that plus our (otherwise non-Hungarian) naming conventions makes it very obvious which are properties. With ReSharper, we can even do things like color-code parameters differently. Personally I find "this" to be noise, and follow ReSharper's suggestions to remove it.

In the end I stand by my answer. While I value readability highly, if you can't quickly and easily determine what a method is doing (this or no this), it's probably too long.

3
  • 3
    Its true you don't need to use this.... but its a better practice, because other people reading your code (and yourself) can see that the property is a member of the class (in one glance), not sure if there are any compiler benefits from this - but I always use this... for properties or fields of the class...
    – JL.
    Sep 12, 2009 at 18:36
  • 1
    There are no compiler benefits but clarity is everything.
    – annakata
    Sep 12, 2009 at 18:38
  • 1
    Agree with JL - using this rarely, if ever, hurts code clarity.
    – Rex M
    Sep 12, 2009 at 18:39
0

For a simple function and regular properties this shouldn't worry you at all.
This does seem like a good question for lambda expressions though, and a quick test shows it might be worth while on that case, if you have a lot of references to properties (this is similar to what was done here):

class Program
{
    public static int Property { get; set; }

    static void Main(string[] args)
    {
        Property = 3;
        int mainVar = Property;

        for (int run = 0; run < 5; run++)
        {
            Stopwatch s = new Stopwatch();
            Action useProperty = () =>
            {
                double res;
                for (int counter = 0; counter < 10000000; counter++)
                    res = Math.Cos(Property);
            };
            s.Start();
            useProperty();
            Console.WriteLine("Property Direct   : {0}", s.Elapsed);
            s.Reset();

            Action useMainVar = () =>
            {
                double res;
                for (int counter = 0; counter < 10000000; counter++)
                    res = Math.Cos(mainVar);
            };
            s.Start();
            useProperty();
            Console.WriteLine("Variable from Main: {0}", s.Elapsed);
            s.Reset();

            Action useLocalVariable = () =>
            {
                int j = Property;
                double res;
                for (int counter = 0; counter < 10000000; counter++)
                    res = Math.Cos(j);
            };
            s.Start();
            useLocalVariable();
            Console.WriteLine("Lambda Local      : {0}", s.Elapsed);
            Console.WriteLine();
        }
        Console.ReadKey();
    }
}

Result:

Property Direct   : 00:00:00.6410370
Variable from Main: 00:00:00.6265704
Lambda Local      : 00:00:00.2793338

Property Direct   : 00:00:00.6380671
Variable from Main: 00:00:00.6354271
Lambda Local      : 00:00:00.2798229

Property Direct   : 00:00:00.6337640
Variable from Main: 00:00:00.6280359
Lambda Local      : 00:00:00.2809130

Property Direct   : 00:00:00.6286821
Variable from Main: 00:00:00.6254493
Lambda Local      : 00:00:00.2813175

Property Direct   : 00:00:00.6279096
Variable from Main: 00:00:00.6282695
Lambda Local      : 00:00:00.2783485
0

Most smaller properties are getting automatically inlined by the JIT, so not much performance gain for that.

For non-trivial properties you will indeed save time when shortening lookup chains. The optimizer is not allowed to remove redundant calls because it cannot determine whether they have side effects or not, otherwise it would change the program's semantics.

There is one exception where you should not save a property value into a local:

for (int i=0; i<myArray.Length; i++) { /* do something with myArray[i] */ }

This pattern is recognized by the JIT and it will automatically remove the array access bounds test.

But if you do this:

int len = myArray.Length;
for (int i=0; i<len; i++) { /* do something with myArray[i] */ }

Then the bounds test cannot be removed since the optimizer can never be sure if the variable len could be tempered with.

So think twice before you "optimize".

1
  • Getting a property once and using that value repeatedly has an advantage besides speed over repeatedly getting the property: if the property is read once to a variable, every use of that variable is guaranteed to yield the same value. By contrast, if code repeatedly calls a property getter, it's possible that the retrieved value might change. To be sure, if the property does change, using an old value might not necessarily be better than using a newer one, but using a consistent value is less likely to introduce Heisenbugs than using inconsistent ones.
    – supercat
    Aug 28, 2014 at 16:25
-1

I did performance calculation, however its tiny, but every tiny bits added can be a huge gain over cpu cycles as you are executing code 100s of times simulatenously in web server, in network activity etc.

C# Local Variable Caching Statistics, you can also download the code and play with it.

8
  • -1 for micro-optimization. Profile your code. I sincerely doubt that you will find property access to be a bottleneck. I/O is usually the culprit.
    – TrueWill
    Sep 12, 2009 at 18:36
  • You can't test this - it depends entirely on what the property get actually does
    – annakata
    Sep 12, 2009 at 18:39
  • 1
    Profileing will not guess what the property is actually doing, you are right in your answer that if property is not really calculating something large everytime its micro micro optimization but you call 100s of properties whose internals you dont know !! How do you make sure?
    – Akash Kava
    Sep 12, 2009 at 18:43
  • 1
    @Akash make sure by using Reflector, or use a tool like RedGate Profiler to see if the call to the property is causing a performance bottleneck.
    – Rex M
    Sep 12, 2009 at 18:45
  • 1
    I know that reality may differ here, but people should not be writing properties that perform large calculations anyway. It is misleading. If you need to perform some intensive calculation, use a method. Sep 12, 2009 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.