Tiny, but unexpected performance difference between variable

Posted by cverbiest on 16-Jul-2015 07:59

In a discussion whether to use a property or a variable, I expected that a variable would be faster than a property as it doesn't have the overhead of setters/getters so I ran a test comparing assigning a variable vs a property.

Turns out that as long as no setters are involved a property is the fastest and a local variable is the slowest. Once the setter is added a property is much slower as expected.

Is this a known difference ?

Before this test I assumed a local variable to be the fastest of them all. Did I make a mistake in my test ?

lqqqqqqqq Information qqqqqqqqqk 
x   106 :Local var x 
x    85 :global var x 
x    55 :property x 
x   522 :property empty set x 
x   559 :property set x 
x qqqqqqqqqqqqqqqqqqqqqqqqqqqq x 


Related  thread  Property or Variable ?

class test:

    &scoped-define testcount 100000
    define variable myVar as char no-undo.

    define private property myProp as char no-undo
        get.
        set.

    define private property mySetPropNocode as char no-undo
        get.
        set (input istring as char):
        end set.

    define private property mySetProp as char no-undo
        get.
        set (input istring as char):
            mySetProp = istring.
        end set.

    constructor test():
        def var i as int no-undo.
        def var localVar as char.
        define variable Results as character no-undo.

        etime(yes).
        do i = 1 to {&testcount}:
            localVar = string(i).
        end.
        results = subst("&1&2 :Local var    ~n", results, string(etime, "zzzzz9")).

        etime(yes).
        do i = 1 to {&testcount}:
            myVar = string(i).
        end.
        results = subst("&1&2 :global var   ~n", results, string(etime, "zzzzz9")).

        etime(yes).
        do i = 1 to {&testcount}:
            myProp = string(i).
        end.
        results = subst("&1&2 :property     ~n", results, string(etime, "zzzzz9")).

        etime(yes).
        do i = 1 to {&testcount}:
            mySetPropNoCode = string(i).
        end.
        results = subst("&1&2 :property empty set ~n", results, string(etime, "zzzzz9")).

        etime(yes).
        do i = 1 to {&testcount}:
            mySetProp = string(i).
        end.
        results = subst("&1&2 :property set ~n", results, string(etime, "zzzzz9")).

        message Results
            view-as alert-box information buttons ok.
    end.

end class.

All Replies

Posted by Peter Judge on 16-Jul-2015 10:58

I thought it was (fairly?) common knowledge that property + mutator or accessor method (aka setter or getter) is a function call, and without it a simple (variable) assignment.
 
I didn't know that property assignment is faster than variable assignment.
 
I made a small tweak to your class (replaced etime with mtime) and have the following result. There's not much to choose between the 3 faster ones (especially given that the number is for 100k iterations) and it seems that the 10x over the slow/function calls is consistent with your numbers.
 
---------------------------
Information (Press HELP to view stack trace)
---------------------------
    52 :Local var    
    42 :global var  
    41 :property    
   366 :property empty set
   395 :property set
---------------------------
OK   Help  

Posted by Thomas Mercer-Hursh on 16-Jul-2015 11:12

I can't imagine that amount of difference mattering for anything.  To me, a property is for something visible outside the class and variable is for something that is not.  I.e., I would never use one for the other regardless.

Posted by TheMadDBA on 16-Jul-2015 11:18

While these kind of timings are interesting in isolation and as an intellectual exercise... I have yet to see an application that benefits from these kind of minor differences over huge samples.

Of course we should all strive to write code that performs as well as possible... there is a practical limit :-)

This thread is closed