It is currently Wed Sep 18, 2019 7:17 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Procedure InBetween faster ?
PostPosted: Wed Jun 26, 2019 2:41 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2012 10:19 am
Posts: 109
Hello, I want to know if a double number (x.d) is in between two integers like that [inf..Sup[

I did a procedure with pb and it works but do you think it's possible to get this procedure faster with assembler (x86 and x64) ?
Code:
RangeInf.i=0
RangeSup.i=6

x0.d=3.12548
x1.d=29.59842

;I want to know if x0 and x1 are in between 0 (or equal to 0) and 5.99999999...
Procedure InBetween(RangeInf.i,RangeSup.i,x.d)
  If Bool(x>=RangeInf And x<RangeSup)
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
 
EndProcedure




Debug InBetween(RangeInf,RangeSup,x0)
Debug InBetween(RangeInf,RangeSup,x1)
Debug InBetween(RangeInf,RangeSup,5.9999999)
Debug InBetween(RangeInf,RangeSup,0.0000001)
Debug InBetween(RangeInf,RangeSup,0)
Debug InBetween(RangeInf,RangeSup,6)
Debug "-----------------------------------"
t1.q=ElapsedMilliseconds()
For i=0 To 1000000
  InBetween(RangeInf,RangeSup,x0)
Next i
t2.q=ElapsedMilliseconds()

Debug (t2-t1) ;310ms x86



Top
 Profile  
Reply with quote  
 Post subject: Re: Procedure InBetween faster ?
PostPosted: Wed Jun 26, 2019 2:53 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1040
Location: right here
Disable the debugger if you are testing performance.
You can try with a macro, but keep in mind that the arguments might be evaluated multiple times.
Code:
CompilerIf #PB_Compiler_Debugger
  CompilerError "disable debugger"
CompilerEndIf

RangeInf.i=0
RangeSup.i=6

x0.d=3.12548
x1.d=29.59842

;I want to know if x0 and x1 are in between 0 (or equal to 0) and 5.99999999...
Procedure InBetween(RangeInf.i,RangeSup.i,x.d)
  If Bool(x>=RangeInf And x<RangeSup)
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
 
EndProcedure

Macro InBetween_macro(RangeInf, RangeSup, x)
  Bool( ((x) >= (RangeInf)) And ((x) < (RangeSup)) )
EndMacro

OpenConsole()

Debug InBetween(RangeInf,RangeSup,x0)
Debug InBetween(RangeInf,RangeSup,x1)
Debug InBetween(RangeInf,RangeSup,5.9999999)
Debug InBetween(RangeInf,RangeSup,0.0000001)
Debug InBetween(RangeInf,RangeSup,0)
Debug InBetween(RangeInf,RangeSup,6)
Debug "-----------------------------------"

t1.q=ElapsedMilliseconds()
For i=0 To 100000000
  InBetween(RangeInf,RangeSup,x0)
Next i
t2.q=ElapsedMilliseconds()

PrintN(Str(t2-t1)) ; 806


t1.q=ElapsedMilliseconds()
For i=0 To 100000000
  InBetween_macro(RangeInf,RangeSup,x0)
Next i
t2.q=ElapsedMilliseconds()

PrintN(Str(t2-t1)) ; 402
Input()




Top
 Profile  
Reply with quote  
 Post subject: Re: Procedure InBetween faster ?
PostPosted: Thu Jun 27, 2019 5:46 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3434
Location: Netherlands
It's very hard to beat a macro like #NULL put together with a procedure, even if it's assembler.

Why do you need it to be faster ?
If you have for example an array of a million double precision values that need to be checked, that can be optimized with assembler.
But in that case you would need a procedure where you pass the array.
When you need to compare a single value, the macro is probably the fastest approach.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye