# PureBasic Forum

 It is currently Tue May 26, 2020 1:08 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Procedure InBetween faster ?Posted: Wed Jun 26, 2019 2:41 pm
 Enthusiast

Joined: Fri Feb 24, 2012 10:19 am
Posts: 158
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

 Post subject: Re: Procedure InBetween faster ?Posted: Wed Jun 26, 2019 2:53 pm

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1238
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

 Post subject: Re: Procedure InBetween faster ?Posted: Thu Jun 27, 2019 5:46 am
 PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3609
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.15 Catalina, PB 5.71 x64

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - IDE    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite