PureBasic Forum
http://forums.purebasic.com/english/

Procedure InBetween faster ?
http://forums.purebasic.com/english/viewtopic.php?f=35&t=73093
Page 1 of 1

Author:  Mesa [ Wed Jun 26, 2019 2:41 pm ]
Post subject:  Procedure InBetween faster ?

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


Author:  #NULL [ Wed Jun 26, 2019 2:53 pm ]
Post subject:  Re: Procedure InBetween faster ?

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



Author:  wilbert [ Thu Jun 27, 2019 5:46 am ]
Post subject:  Re: Procedure InBetween faster ?

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.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/