Friday, 13 January 2012

setter perfomance test, conclusions

So far the accessor tests I've been doing have only been of getters, or more generally of accessor functions for retrieving members from classes. The obvious question is whether setters have the same performance penalty. The answer is yes, they do.

The code uses the same classes as Wednesday's post. The only difference is the tests write to rather than read from the members, like this.

function Test1():void {
    var iTrial:int, v:Vec2Da;
    for (iTrial = 0; iTrial < kNumTrials; iTrial++) {
        v = aDataA[iTrial];
        v.iX = v.iY = iTrial;
        v.iX = v.iY = iTrial;
        v.iX = v.iY = iTrial;
        v.iX = v.iY = iTrial;

The test app is available here. As with getters the two test functions are the same except for the data type. And, as with getters, using setters is much slower than accessing members directly – at least three times as slow. The performance difference does not seem as extreme as reading members, but this may be as writing data e.g. to memory is generally more expensive.

In conclusion:
  • avoid accessor functions when performance matters
  • be especially careful with setters and getters
  • know, or know when to check, which built-in classes use setters and getters

No comments:

Post a comment