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.
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.
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 :-)