none
What is recomended to use: Property access or create a new variable? RRS feed

  • Question


  • i.e. what takes more performance, the property access or creating a new variable in memory.
    VB an C# examples below:

    VB example:
    Public Class Sample  
        Public Shared Sub DoAction()  
            Dim test As New List(Of Test)  
            test.Add(New Test("test"))  
            'Here I have to assign multiple fields (in my case it's Label.Text fields etc.) to one variable:  
     
            'Way 1: assign by property  
            Dim a = test(0).Prop  
            Dim b = test(0).Prop  
            Dim c = test(0).Prop  
     
            'Way 2: assign by creating a new variable:  
            Dim prop = test(0).Prop  
            Dim d = prop  
            Dim e = prop  
            Dim f = prop  
        End Sub 
    End Class 
     
    Public Class Test  
        Private m_Prop As String 
        Public Property Prop() As String 
            Get 
                Return m_Prop  
            End Get 
            Set(ByVal value As String)  
                m_Prop = value  
            End Set 
        End Property 
        Public Sub New(ByVal prop As String)  
            m_Prop = prop  
        End Sub 
    End Class 

    or C# example:
    public class Sample  
    {  
        public static void DoAction()  
        {  
            List<Test> test = new List<Test>();  
            test.Add(new Test("test"));  
            //Here I have to assign multiple fields (in my case it's Label.Text fields etc.) to one variable:   
     
            //Way 1: assign by property   
            var a = test[0].Prop;  
            var b = test[0].Prop;  
            var c = test[0].Prop;  
     
            //Way 2: assign by creating a new variable:   
            var prop = test[0].Prop;  
            var d = prop;  
            var e = prop;  
            var f = prop;  
        }  
    }  
     
    public class Test  
    {  
        private string m_Prop;  
        public string Prop  
        {  
            get { return m_Prop; }  
            set { m_Prop = value; }  
        }  
        public Test(string prop)  
        {  
            m_Prop = prop;  
        }  
    }  
     


    Shimmy
    Tuesday, December 2, 2008 11:39 AM

Answers

  • Hello,

    You won't notice any performance improvement if you declare local variable unless you perform some additional operations in your property getter.
    When you declare local variable you just reference the same object in managed heap.

    In this case, you should base your decision on: in which way (1 or 2) your code will be more nice-looking, but not on performance considerations.

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Tuesday, December 2, 2008 1:05 PM
  • In this very specific case, copying the local variable is faster than indexing the list again.  The JIT optimizer doesn't fold the list indexing operation, it doesn't have the horse power to check if the List<> indexer has any side-effects that should be considered.  Personally, that's also what I would do when coding style is the issue.  But it is a very synthetic code sample.
    Hans Passant.
    Tuesday, December 2, 2008 1:21 PM
    Moderator
  • Hmya, you are comparing apples and oranges.  Dereferencing a class object and accessing a property are two different things.  Accessing a property almost always *also* requires dereferencing an object.

    You are taking the wrong approach, Donald Knuth's famous words: "premature optimization is the root of all evil".  Write your code with all due regard to readability.  Find out what needs to be optimized after your done writing it, use a profiler to find out where the hot spots of your code are located.  It is very likely not where you expect it to be.

    Hans Passant.
    Tuesday, December 2, 2008 1:59 PM
    Moderator

All replies

  • Hello,

    You won't notice any performance improvement if you declare local variable unless you perform some additional operations in your property getter.
    When you declare local variable you just reference the same object in managed heap.

    In this case, you should base your decision on: in which way (1 or 2) your code will be more nice-looking, but not on performance considerations.

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Tuesday, December 2, 2008 1:05 PM
  • In this very specific case, copying the local variable is faster than indexing the list again.  The JIT optimizer doesn't fold the list indexing operation, it doesn't have the horse power to check if the List<> indexer has any side-effects that should be considered.  Personally, that's also what I would do when coding style is the issue.  But it is a very synthetic code sample.
    Hans Passant.
    Tuesday, December 2, 2008 1:21 PM
    Moderator
  • OK, forget the fact that it was an indexer, assume it's a simple class.

    I'm dubting between the readability of the code and the performance.

    so now I have 2 questions:
    1. does a reference to a class (a pointer) cost more than a simple property accessing (and if we are talking about asp, consider that we also have to access ViewState in many cases)?
    2. what about a variable or a structure, which when I create a variable it's not a pointer but the value is copied?

    thanks for you help you two.


    Shimmy
    Tuesday, December 2, 2008 1:41 PM
  • Hmya, you are comparing apples and oranges.  Dereferencing a class object and accessing a property are two different things.  Accessing a property almost always *also* requires dereferencing an object.

    You are taking the wrong approach, Donald Knuth's famous words: "premature optimization is the root of all evil".  Write your code with all due regard to readability.  Find out what needs to be optimized after your done writing it, use a profiler to find out where the hot spots of your code are located.  It is very likely not where you expect it to be.

    Hans Passant.
    Tuesday, December 2, 2008 1:59 PM
    Moderator