Procedure InBetween faster ?

Bare metal programming in PureBasic, for experienced users
Mesa
Enthusiast
Enthusiast
Posts: 226
Joined: Fri Feb 24, 2012 10:19 am

Procedure InBetween faster ?

Post by Mesa »

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: Select all

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

#NULL
Addict
Addict
Posts: 1335
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Procedure InBetween faster ?

Post by #NULL »

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: Select all

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


wilbert
PureBasic Expert
PureBasic Expert
Posts: 3734
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Procedure InBetween faster ?

Post by wilbert »

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.15 Catalina, Windows 10
Post Reply