The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Gunner on May 30, 2011, 12:44:44 AM

Title: Use a loop or multiple api calls
Post by: Gunner on May 30, 2011, 12:44:44 AM
Would it be better to use a loop to call say LoadImage (or any other api you might call a few times), or use multiple invokes?  The loop would be better right?  smaller code and maybe faster?

For instance:

    ; ##### Tab control imagelist
    invoke  ImageList_Create, 16, 16, ILC_MASK or ILC_COLORDDB, 5, 5
    mov     ebx, eax

    mov      edi, ICON_2
    .while   edi <= ICON_6
        invoke  LoadImage, esi, edi, IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT or LR_SHARED   
        push   eax                                             
        invoke  ImageList_AddIcon, ebx, eax                     
        pop      eax                                             
        invoke  DestroyIcon, eax   
        inc      edi
    .endw
    mov      hImlTabs, ebx


That would be the preferred way of doing it right?  Instead of calling LoadImage, ImageList, and DestroyIcon 5 times...  Looking over some old code and looking to make it smaller/faster...
Title: Re: Use a loop or multiple api calls
Post by: qWord on May 30, 2011, 12:50:13 AM
IMO it is a question of your personal taste - API calls are mostly 'relative slow', so it doesn't matter wether to call them in an loop or not.
(maybe it more the question about readability)
Title: Re: Use a loop or multiple api calls
Post by: dedndave on May 30, 2011, 01:07:37 AM
loop it   :P
the speed advantage you might gain by unrolling the loop would be negligible compared to the calls, themselves
so - when you can't write for speed, write for small - that's my motto
Title: Re: Use a loop or multiple api calls
Post by: hutch-- on May 30, 2011, 01:25:14 AM
Rob,

Think of the next time you must edit this code before you design it as a loop. The size gain is trivial and the speed gain could not be measured but as loop code it will be a lot more work to change and all for no purpose. Inline it and it remains easy to edit in the future.
Title: Re: Use a loop or multiple api calls
Post by: Tedd on June 02, 2011, 12:27:19 PM
The loop is easier to maintain - if you change the number of images to be loaded, it's one simple change.
It's also smaller and tidier. Speed isn't really a concern when the biggest delay comes from the API calls.
Title: Re: Use a loop or multiple api calls
Post by: sinsi on June 02, 2011, 12:52:54 PM
  pop eax
  invoke  DestroyIcon, eax

Too slow...
  call DestroyIcon
:bdg
Title: Re: Use a loop or multiple api calls
Post by: drizz on June 02, 2011, 05:49:54 PM
Multiple calls can be easy to maintain too.

i = ICON_2
while i le ICON_6
invoke LoadImage, esi, i, IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT or LR_SHARED   
mov edi,eax
invoke ImageList_AddIcon, ebx, eax
invoke DestroyIcon, edi
i = i + 1
endm

or like:
;(useful when identifiers aren't ordered)
for iconId,<ICON_2,ICON_99,ICON_3,ICON_15,ICON_6>
invoke LoadImage, esi, iconId, IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT or LR_SHARED   
mov edi,eax
invoke ImageList_AddIcon, ebx, eax
invoke DestroyIcon, edi
endm

Runtime loop is better since it saves space and speed is irrelevant when calling 95.01% of the api.
When optimizing, less code/data can mean more speed if you picture in the cache.

Title: Re: Use a loop or multiple api calls
Post by: jj2007 on June 02, 2011, 06:54:40 PM
Example of a runtime loop - very simple & compact:
mov edi, offset CtHandles ; ---- setup and create the controls ----
NumCtrl=3
IdCt0=120
xor ebx, ebx
.Repeat
lea edx, [ebx+IdCt0]  ; 120, 121, ...
invoke CreateWindowEx, WS_EX_TOOLWINDOW,
   offset ClassStatic, 0,
   WS_CHILD or WS_VISIBLE or SS_NOTIFY or WS_THICKFRAME, 0, 0, 0, 0,
   hWnd, edx, hInstance, NULL
stosd
inc ebx
.Until ebx>=NumCtrl