News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Use a loop or multiple api calls

Started by Gunner, May 30, 2011, 12:44:44 AM

Previous topic - Next topic

Gunner

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...
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

qWord

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)
FPU in a trice: SmplMath
It's that simple!

dedndave

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

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

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.
No snowflake in an avalanche feels responsible.

sinsi

  pop eax
  invoke  DestroyIcon, eax

Too slow...
  call DestroyIcon
:bdg
Light travels faster than sound, that's why some people seem bright until you hear them.

drizz

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.

The truth cannot be learned ... it can only be recognized.

jj2007

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