Inline ASM slower than equivalent PB code...

Everything else that doesn't fall into one of the other PB categories.
LJ
Enthusiast
Enthusiast
Posts: 177
Joined: Wed Apr 30, 2003 4:00 pm

Matthew/Jack

Post by LJ »

Sure would be great if you guys could post an article in the Tricks n Tips section under the Purebasic Inline Assembly sticky. Something that would be appropriate for beginners with explanation would be great.

Lj
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Is there a microsecond counter available to PB?
No, but the Windows API has one :

Code: Select all

ticks = GetTickCount_()
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Flype wrote:
Is there a microsecond counter available to PB?
No, but the Windows API has one :

Code: Select all

ticks = GetTickCount_()
That's a millisecond counter and not a microsecond counter. Search the forum, i think there's a highres timer user library out there made by Danilo, if i'm not misstaken..
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

ok this is an old post, but i would like to finnish it up.
Maybe it'll help other people too.
I made some things with the source, and i made the asm
faster than the pb version.

Debugger should be off.
Copy/paste source to your editor and run without debugger, then it should
show you. The tests may take a while on slow computers.
Well, the source:

Code: Select all

;Modulus test
;Code by: Daniel Middelhede    -THEFOOL-
;NOTE: do not run with debugger on.
;And: run with inline asm enabled!

;And in case u dont turn off the debugger :P
Debug "Please turn of debugger!"
CallDebugger


;Just some info and opening of console..
OpenConsole()
PrintN("Modulus test")
PrintN("Copyright(c) 2004 Daniel Middelhede   -THEFOOL-")
PrintN("Please close all other programs while running this!")
PrintN("")
PrintN("3 seconds to go..")
Delay(3000)
PrintN("Tests started!")
PrintN("")

;Change DIVIDEND and DIVISOR to change the division. But make sure the dividend and divisor is
;the same in the other part of code, the PB test!
dividend=100
divisor=42
time1=GetTickCount_()

MOV ebx, divisor ;Make diviser declared outside loop because it wont change!
For i=1 To 200000000 ;loop a lot.
  ;Ok. This is the ASM from the first post, wich i took and optimized a bit
  ;with making it direct.
  
  MOV eax, dividend 
  !CDQ 
  !IDIV ebx 
  MOV dividend, edx 
  
Next i
mytime= GetTickCount_()-time1
PrintN("PB Inline ASM modulus")
PrintN("Result: "+Str(dividend))
PrintN("Time  : "+Str(mytime))

;**OK. Now we go to the PB test!
PrintN("")
divisor=42
dividend=100
time2=GetTickCount_()

divisor=42 ;Make diviser declared outside loop because it wont change!
For a=1 To 200000000 ;Loop alot.
  ;This is the PB code for modulus!
  
  dividend=100
  dividend - ((dividend / divisor) * divisor)
  
Next a
mytime2= GetTickCount_()-time2
PrintN("PB way modulus")
PrintN("Result: "+Str(dividend))
PrintN("Time  : "+Str(mytime2))


PrintN("")
PrintN("Judgement:")
;**Ok this is the final judgement. This test will see if PB or ASM was faster.
If mytime<mytime2
  PrintN("ASM won with: "+Str(mytime2-mytime)+" Milliseconds!")
Else
  PrintN("PB won with: "+Str(mytime-mytime2)+" Milliseconds!")
EndIf
PrintN("")
PrintN("Press enter key to exit the test!")
Input()
Ill say it once more. Run WITHOUT debugger!

Cheers!
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

@thefool,

Add this part to your code ...

Code: Select all

;**OK. Now we go to the FASM test! 
PrintN("") 
divisor=42 
dividend=100 
time3=GetTickCount_() 

!  MOV     ecx, 1 ; For a=1 To Count ;Loop alot. 
!  MOV     ebx, 42 ; divisor=42 ;Make diviser declared outside loop because it wont change! 
! _For4:
!  MOV     eax, 100 ; dividend = 100
!  CDQ
!  IDIV     ebx
!  MOV     dword [v_dividend], edx
!  INC      ecx ; Next a 
!  CMP     ecx, dword [v_Count]
!  JLE       _For4

mytime3= GetTickCount_()-time3
PrintN("PB way modulus") 
PrintN("Result: "+Str(dividend)) 
PrintN("Time  : "+Str(mytime3)) 
optimization, remember, optimization !

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

@fweil I'm afraid your code brings up an error box for me.
PureBasic.asm [201]: CMP ecx, dword [v_Count]
error: undefined symbol.
I'm afraid I dont know enough about FASM to know whether its your code or an error with my system or anything.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

GreenGiant,

You are right. You have to declare a Count.l = something so that this variable exist formerly. This is the number of times you want the loop to be executed.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

thx to fweil.

The reason i posted this was because i coultn see it finnished. But its great to get even more speed.

something about your code: The loops are need to be in the same code.
An for..next loop. Because, that shouldnt change. I cant think of any
point except speed testing where the loop code should be faster.
And, the reason i didnt use mov eax,100 etc.. is that the end user should be able to change it. But anyway, thanks.
Post Reply