Sunday, October 23, 2005

Further Optimization Analysis

Calvin made a post about code optimization and noted that if you actually attempt to do a
FOR i = 1 TO num
IF wParam=ASC("a")
wParam=ASC("b")
ENDIF
ENDFOR

It would take longer than just using the direct values. So what happens if you used variables or #DEFINE statements.

I first thought "i'll use constants" , adding the following

#DEFINE cn1 ASC('a')
#DEFINE cn2 ASC('b')

and changing the logic to:
FOR i = 1 TO num
IF wParam=cn1
wParam=cn2
ENDIF
ENDFOR

The Result? A little faster but nowhere close to the performance improvements gained by using the literal values. Why? Because it is simply replacing the cn1 and c2 with the ASC('97') statements.

But even when I changed the #DEFINEs to variables and assigned the values, they still weren't improved.

The only time it came close was if I explicitly did
#DEFINE cn1 78
#DEFINE cn2 79

Here's a basic breakdown (non scientific) of my times:
With literal values: 3.781 seconds
With functions: 5.187
With variables: 5.11
With DEFINEs and Functions: 4.98 - 5 (I did run it twice)
With DEFINEs and Literals: 3.782 seconds

So the rule has to be - if you can avoid using functions and variables by using constants or the literal values, do it.

Optimization...go figure.

No comments: