Page 1 of 2

[Implemented] switch var1, var2

Posted: Tue Mar 02, 2004 7:53 pm
by MLK

Code: Select all

x=1
y=2
switch x,y ;or however
;x=2
;y=1

Posted: Tue Mar 02, 2004 8:44 pm
by LarsG
why not use a buffer?!?
if you sorely need it for speed issues, I'm sure you could whip together some asm to fix it?!?
(atleast someone can.. I'm sure of it)

Posted: Tue Mar 02, 2004 8:54 pm
by GPI

Code: Select all

Procedure SwapL(*Value1.LONG,*Value2.LONG); swap two long-variables
  save.l=*Value1\l
  *Value1\l=*Value2\l
  *Value2\l=save
EndProcedure

a.l=1
b.l=2

swapl(@a,@b)
debug a
debug b

Posted: Tue Mar 02, 2004 10:31 pm
by BillNee
Is there any easy way to swap array locations? I've tried pointers but swapping them doesn't do anything. Guess the arrays new address has to go somewhere in the array. I'd really like to swap a.w(100,100) and b.w(100,100) for example. Right now I use copymemory and then reDim b if necessary. Since copymemory does it by bytes, I thought a swap might be easier/quicker. I'm looking for something like the old GFA32 command Swap a(),b(). Thanks for any help with this
BTW i really searched the search files but couldn't find this request.
Bill Nee

Posted: Wed Mar 03, 2004 12:32 am
by freak
The procedure by GPI is really slow.
Simply doing a "c=a: a=b: b=c" is much faster.
The fastest is probably this:

Code: Select all

a = 1
b = 2

PUSH a
PUSH b
POP a
POP b

Debug a
Debug b
(with InlineASM enabled)
Doesn't work inside a procedure though.

I still vote for a built-in swap function (which probably wouldn't be faster,
but hey, i'm lazy :P )

About swaping the array elements:
You can't swap any pointers here. You have to really move the data from
the one array to the other. In case of just one variable, you'll probably
be better off not using CopyMemory() but just move the values to a
temp variable and then back.
When you want to swap structures, CopyMemory() is probably the best.

Timo

Posted: Wed Mar 03, 2004 7:02 pm
by horst
a little faster: :?

Code: Select all

a = 1 
b = 2 

MOV eax,a
XCHG eax,b
MOV a,eax

Debug a 
Debug b 

Posted: Wed Mar 03, 2004 7:20 pm
by El_Choni
horst wrote:a little faster
You mean, slower:

Code: Select all

a = 1
b = 2

InitialTime = GetTickCount_()
For i=0 To 1000000
  PUSH a
  PUSH b
  POP a
  POP b
Next i
Debug GetTickCount_()-InitialTime

InitialTime = GetTickCount_()
For i=0 To 1000000
  MOV eax, a
  XCHG eax, b
  MOV a, eax
Next i
Debug GetTickCount_()-InitialTime

Posted: Wed Mar 03, 2004 9:38 pm
by coma

Code: Select all

a = 1 : b = 2
InitialTime = GetTickCount_() 
For i=0 To 10000000 
    c=a
    a=b
    b=c
Next i 
t0 = GetTickCount_()-InitialTime 

InitialTime = GetTickCount_() 
For i=0 To 10000000 
    PUSH a 
    PUSH b 
    POP a 
    POP b 
Next i 
t1 = GetTickCount_()-InitialTime 


InitialTime = GetTickCount_() 
For i=0 To 10000000 
    MOV eax, a 
    XCHG eax, b 
    MOV a, eax 
Next i 
t2 = GetTickCount_()-InitialTime 

InitialTime = GetTickCount_() 
For i=0 To 10000000 
    MOV eax,a 
    MOV ebx,b 
    MOV b,eax 
    MOV a,ebx 
Next 
t3 = GetTickCount_()-InitialTime 

MessageRequester("result",Str(t0)+" / "+Str(t1)+" / "+Str(t2)+" / "+Str(t3),0)

Posted: Wed Mar 03, 2004 9:54 pm
by horst
amazing :o

Posted: Thu Mar 04, 2004 1:07 am
by blueznl
so much for a little swap statement? uh...

:P

but euh, a swap statement would make nice readable code, yeah, i miss it as well

Posted: Thu Mar 04, 2004 3:51 am
by El_Choni
Ok, it wasn't a pro benchmark, just wanted to state that *it's not faster* XD

Posted: Thu Mar 04, 2004 10:35 am
by coma
your benchmark was good, it's just that with debugger on, on my pc, the results was in reverse order.

Posted: Thu Mar 04, 2004 11:52 am
by Psychophanta
Curious!
On an opteron 3000+, the example posted by coma, with debugger off and with inline-ASM off, this is:

Code: Select all

a = 1 : b = 2 
InitialTime = GetTickCount_() 
For i=0 To 10000000 
    c=a 
    a=b 
    b=c 
Next i 
t0 = GetTickCount_()-InitialTime 

InitialTime = GetTickCount_() 
For i=0 To 10000000 
    !PUSH dword[v_a]  
    !PUSH dword[v_b]  
    !POP dword[v_a]  
    !POP dword[v_b]  
Next i 
t1 = GetTickCount_()-InitialTime 


InitialTime = GetTickCount_() 
For i=0 To 10000000 
    !MOV eax,dword[v_a] 
    !XCHG eax,dword[v_b] 
    !MOV dword[v_a], eax 
Next i 
t2 = GetTickCount_()-InitialTime 

InitialTime = GetTickCount_() 
For i=0 To 10000000 
    !MOV eax,dword[v_a] 
    !MOV ebx,dword[v_b] 
    !MOV dword[v_b] ,eax 
    !MOV dword[v_a] ,ebx 
Next 
t3 = GetTickCount_()-InitialTime 

MessageRequester("result",Str(t0)+" / "+Str(t1)+" / "+Str(t2)+" / "+Str(t3),0)
result is: 110 / 94 / 125 / 46

Posted: Thu Mar 04, 2004 12:02 pm
by coma
try this :

Code: Select all

a = 1 : b = 2 

InitialTime = GetTickCount_() 
For i=0 To 10000000 
    !MOV eax,dword[v_a] 
    !MOV ebx,dword[v_b] 
    !MOV dword[v_b] ,eax 
    !MOV dword[v_a] ,ebx 
Next 
t0 = GetTickCount_()-InitialTime 

InitialTime = GetTickCount_() 
For i=0 To 10000000 
Next 
t1 = GetTickCount_()-InitialTime 

MessageRequester("result",Str(t0)+" / "+Str(t1),0)

Posted: Thu Mar 04, 2004 2:00 pm
by freak
I don't get the last two posts. What is so special about these results?

Psychophanta:
You code is just coma's without InlineASM. but the generated ASM output
is exactly the same, so speeds are exactly the same... what's the point?
(that is only true without debugger though. debugged inlineasm is slower)

coma:
What's this about? A loop without content is faster than if you put something
inside... so what? That's obvious.

just curious...

Timo