[Implemented] switch var1, var2

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

[Implemented] switch var1, var2

Post by MLK »

Code: Select all

x=1
y=2
switch x,y ;or however
;x=2
;y=1
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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)

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post 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
BillNee
User
User
Posts: 93
Joined: Thu Jan 29, 2004 6:01 am
Location: Homosassa, FL

Post 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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
horst
Enthusiast
Enthusiast
Posts: 197
Joined: Wed May 28, 2003 6:57 am
Location: Munich
Contact:

Post 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 
Horst.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post 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
El_Choni
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post 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)
horst
Enthusiast
Enthusiast
Posts: 197
Joined: Wed May 28, 2003 6:57 am
Location: Munich
Contact:

Post by horst »

amazing :o
Horst.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Ok, it wasn't a pro benchmark, just wanted to state that *it's not faster* XD
El_Choni
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

your benchmark was good, it's just that with debugger on, on my pc, the results was in reverse order.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post 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)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
Post Reply