### Author Topic: (Optimization) Use IIF& () or better IF .. THEN ?  (Read 3702 times)

0 Members and 1 Guest are viewing this topic.

#### Theo Gottwald ##### (Optimization) Use IIF& () or better IF .. THEN ?
« on: August 06, 2007, 09:13:44 AM »
Very short code in Basic:

IIF&(R01,10,11)

Which will do just the same as this:

IF R01 THEN R02=10& ELSE R02=11&

Lets take a look at the ASM-Level if there are differences.

EXAMPLE 1: IF .. THEN / IIF& using LONG.
We can see that the IF ... THEN makes better use of REGISTER VARIABLES, if they are used.

Code: [Select]
`SUB TestfuncA()REGISTER R01 AS LONG,R02 AS LONG    !NOPR01=100    !NOPR02=IIF&(R01,10&,11&)    ! NOP    IF R01 THEN R02=10& ELSE R02=11&    !NOPEND SUB' becomes4023CE  NOP4023CF  MOV ESI, DWORD 000000644023D5  NOP'--------------------------------------------------------' This is the IIF&'--------------------------------------------------------4023D6  MOV EAX, ESI4023D8  TEST EAX, EAX4023DA  JZ  SHORT L4023E34023DC  MOV EAX, DWORD 0000000A4023E1  JMP SHORT L4023E84023E3  MOV EAX, DWORD 0000000B4023E8  MOV EDI, EAX4023EA  NOP'--------------------------------------------------------' This is the IF ... THEN'--------------------------------------------------------4023EB  MOV EAX, ESI4023ED  TEST EAX, EAX4023EF  JZ  SHORT L4023F94023F1  MOV EDI, DWORD 0000000A4023F7  JMP SHORT L4023FF4023F9  MOV EDI, DWORD 0000000B4023FF  NOP`
EXAMPLE 2: Same code - just normal local variables (no REGISTER variables).
Even in this case, the IF ... THEN is slightly better optimized.
Lets say that when using IIF& the difference is not really big.
This will change in exmple 3.

Code: [Select]
`SUB TestfuncA()#REGISTER NONELOCAL R01 AS LONG,R02 AS LONG    !NOPR01=100    !NOPR02=IIF&(R01,10&,11&)    ! NOP    IF R01 THEN R02=10& ELSE R02=11&    !NOPEND SUB  4023CC  NOP4023CD  MOV DWORD PTR [EBP+FFFFFF78], DWORD 000000644023D7  NOP'--------------------------------------------------------' This is the IIF&'--------------------------------------------------------4023D8  MOV EAX, DWORD PTR [EBP+FFFFFF78]4023DE  TEST EAX, EAX4023E0  JZ  SHORT L4023E94023E2  MOV EAX, DWORD 0000000A4023E7  JMP SHORT L4023EE4023E9  MOV EAX, DWORD 0000000B4023EE  MOV DWORD PTR [EBP+FFFFFF74], EAX4023F4  NOP'--------------------------------------------------------' This is the IF ... THEN'--------------------------------------------------------4023F5  MOV EAX, DWORD PTR [EBP+FFFFFF78]4023FB  TEST EAX, EAX4023FD  JZ  SHORT L40240B4023FF  MOV DWORD PTR [EBP+FFFFFF74], DWORD 0000000A402409  JMP SHORT L40241540240B  MOV DWORD PTR [EBP+FFFFFF74], DWORD 0000000B402415  NOP`
EXAMPLE 3:
`'--------------------------------------------------------' This is the IIF()'--------------------------------------------------------4023D6  MOV EAX, ESI4023D8  TEST EAX, EAX4023DA  JZ  SHORT L4023E44023DC  FILD INTEGER PTR 4023E2  JMP SHORT L4023EA4023E4  FILD INTEGER PTR 4023EA  FISTP QUAD PTR [EBP-5C]4023ED  MOV EDI, DWORD PTR [EBP-5C]4023F0  NOP`