News:

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

What is faster

Started by Manos, February 09, 2005, 08:22:27 PM

Previous topic - Next topic

Manos

What is faster ?
To use a local variable to preserve a value
or to use the pair push-pop ?

For example:

;-------------First way-------------
LOCAL num:DWORD

invoke SomeProc
mov num,eax

invoke Send Message,hwnd1,WM_XXX,num,0
invoke SendMessage,hwnd2,WM_XXX,num,0

;------------------------------------

;---------Second way--------------

invoke Someproc
push eax
invoke Send Message,hwnd1,WM_XXX,eax,0
pop eax
invoke SendMessage,hwnd2,WM_XXX,eax,0

;----------------------------

Regards,
Manos.


hutch--

Manos,

because I am lazy I would probably use the register trick but safety in complex code says use the variable. I doubt that speed is an issue when there are a number of API calls being made.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

P1

For quickies, registers is the way to go.

invoke Someproc
mov ebx,eax
invoke Send Message,hwnd1,WM_XXX,eax,0
mov eax,ebx
invoke SendMessage,hwnd2,WM_XXX,eax,0

I don't know how tight your coding, but if speed is the name of the game, don't go to memory.

Regards,  P1  :8)

Manos

P1,
I can not use esi,edi,ebx because I use these in other part of code.
I think that,as hutch-- told,the cost is the same because I call API.

Thanks both,
Manos.

zooba

Its not that the cost is the same, its just that the cost of API calls is so high the immesurable small time difference between pushing/popping and moving to or from memory is not worth worrying about and you'd be better to find a way around the API call if possible.

Using a memory variable is much easier to keep track of as the stack is organised for you, and keep in mind that windows API calls always preserve ebx, edi and esi.

Manos

zooba,
when I say that the cost is the same,I mean that  the cost of API calls is so high,
so that the small time difference between pushing/popping and moving to or from memory
is negligible.
I now that API calls always preserve ebx, edi and esi,but I use these in structures:
ASSUME ebx:.........

Manos.

Mirno

If you're a masochist, or hate the person who will maintain the code you can avoid all speed loss!


push 0
push eax
push WM_XXX
push hwnd2
push 0
push eax
push WM_XXX
push hwnd1
call SendMessage
call SendMessage


If you release code like this don't be surprised if someone comes around your house and kneecaps you.

Mirno

petezl

I get problems when running out of registers.
I like to push some tempory values rather than cluttering with local variables. Just so long as it's decipherable after a break away from it (really needs good commenting). But then, I'm not an optomisation guru.
Peter.
Cats and women do as they please
Dogs and men should realise it.

P1

Quote from: Manos on February 10, 2005, 07:00:11 AMI can not use esi,edi,ebx because I use these in other part of code.
Then there's edx & ecx.  The idea is when clock cycles count, then count your cycles for each coding option.

As far as, whether this will count for much, of course that debatable.  But I just gave an answer to the question.

Regards,  P1  :8)

Relvinian

Quote from: Mirno on February 10, 2005, 11:23:09 AM
If you're a masochist, or hate the person who will maintain the code you can avoid all speed loss!


push 0
push eax
push WM_XXX
push hwnd2
push 0
push eax
push WM_XXX
push hwnd1
call SendMessage
call SendMessage


If you release code like this don't be surprised if someone comes around your house and kneecaps you.

Mirno

I've done that a few times when I didn't like the options of storing the value somewhere when calling 2+ API calls that needed some previous register values. It is a little different but personally I don't see anything wrong with it as long as you comment the code so yourself or others can see what you've done.

Relvinian

zooba

push 0
push eax
push WM_XXX
push hwnd2
push here
push 0
push eax
push WM_XXX
push hwnd1
push SendMessage
jmp SendMessage

here:


Any reason why this code wouldn't work? I haven't tried it, it just occurred to me that forcing the API to ret to itself would (may?) be quicker than doing a ret followed by another call.

OceanJeff32

Hey Manos! I've used the MMX, and XMM registers for storing data, because these registers are closer to the CPU, they are quicker to retrieve data from.  (Correct me if I'm wrong on this)  There are also special commands to retrieve just the amount of data you store, so you can use these locations to store multiple data items.  Especially the huge 64-bit MMX registers, and 128-bit XMM registers.

Plus, I also think the MMX/FPU registers are actually 80-bit...but I'm not sure on this point...according to the Intel Manuals they are 80-bit registers.

Later,

Jeff C
:P
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

hutch--

Its usually the case that if there is some gain, you do it but with a sequence of API calls, ther is no measurable gain where putting the data into stack variables is both safe and maintainable.

If there is a speed consideration that is worth the effort, trash ESP, jump into the middle of the guts of the API or any other seriously dangerous practice but if you have to pass the code around and it has to be reliable, do it the safe way as it will preserve your kneecaps.  :toothy
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Mark_Larson

Quote from: OceanJeff32 on February 26, 2005, 02:23:02 AM
Hey Manos! I've used the MMX, and XMM registers for storing data, because these registers are closer to the CPU, they are quicker to retrieve data from.  (Correct me if I'm wrong on this)  There are also special commands to retrieve just the amount of data you store, so you can use these locations to store multiple data items.  Especially the huge 64-bit MMX registers, and 128-bit XMM registers.

  You're wrong.  Try downloading and reading the P3 and P4 optimization manual.  They go into a lot more detail of how the processor works.  And it will help your optimization skills a lot.  Like with most optimizations sometimes a PUSH/POP is faster and sometimes saving it to an MMX register is faster.  So you always need to time your code.  It is more expensive time-wise to use the XMM registers over the MMX registers.
BIOS programmers do it fastest, hehe.  ;)

My Optimization webpage
htttp://www.website.masmforum.com/mark/index.htm