• Welcome to Theos PowerBasic Museum 2017.

News:

Attachments are only available to registered users.
Please register using your full, real name.

Main Menu

(Optimization) Don't use RESET if you can use (=0)

Started by Theo Gottwald, January 02, 2007, 09:37:57 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Theo Gottwald

It looks that easy:

RESET C01,C28   
and the PB-help file says:

QuoteIf variable is numeric, it is set to zero.

With lots of variables the code looks even shorter then:
C01=0:C28=0

On a second look we see that RESET is a command with multiple uses.
It can be used on Variants, UDTs, Arrays etc.
And commands with multiple uses often have less optimized results after compiling.
Just because they are universal.

In the case of RESET we get this ASM-Code:


47F138 BB3CB54800             MOV EBX, DWORD 0048B53C
47F13D B800060000             MOV EAX, DWORD 00000600
47F142 E831170000             CALL L480878
47F147 BB3CD44800             MOV EBX, DWORD 0048D43C
47F14C B804000000             MOV EAX, DWORD 00000004
47F151 E822170000             CALL L480878
'
' and thats NOT ALL. every CALL here calls this subroutine.
'
480878 57                     PUSH EDI
480879 FC                     CLD
48087A 33C9                   XOR ECX, ECX
48087C 91                     XCHG  EAX, ECX
48087D 8BFB                   MOV EDI, EBX
48087F D1E9                   SHR ECX, 1
480881 F366AB                 REPE: STOSW
480884 80D100                 ADC CL, BYTE 00
480887 F3AA                   REPE: STOSB
480889 5F                     POP EDI
48088A C3                     RET NEAR
'


Thats quite a lot! Just for setting a DWORD or LONG to zero!
Thats why RESET should be reserved for cases where we really need it. For Arrays, UDT's etc.

Now lets take a look on what we get with a simple "=0".
We take just:

Local C28 AS LONG
C28=0


Don't be surprised, its just one statement left:

47F10B    MOV DWORD PTR [0048D43C], DWORD 00000000

Thats why a simple "=0" should be prefered in optimized code.