Which code block below is better to use, when considering final exe size and
speed of the procedure? I'd say the second is better due to far less code, but
it also creates a new variable (r), so how can I know for sure? Thanks.
Procedure IsNumberLessThan1002(n)
If n < 100
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
Also take into account whether the majority of numbers are less than 100. If the majority is 100 or more, then this is much faster because the forward jump isn't taken:
Okay, the reason I asked is because I read somewhere once that a procedure
should only ever have 1 x ProcedureReturn, and I loaded one of my old sources
and saw 2 x ProcedureReturns in there, and wondered if it was worth changing
it to only one, like in my code examples in this topic. Any ideas on this?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Procedure.l IsNumberLessThan100(n)
If n<100
ProcedureReturn #True
EndIf
EndProcedure
Debug IsNumberLessThan100(50)
Letting the procedure return naturally without ProcedureReturn returns #False, has worked in all versions so far, though I'm not sure this can be counted on in the future? Fred/Freak?
Not sure if it's shorter or faster, but personally I prefer it with only one ProcedureReturn.
Procedure.l IsNumberLessThan100_0(n.l)
If n<100
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure.l IsNumberLessThan100_1(n.l)
If n<100 : ProcedureReturn #True : EndIf
EndProcedure
Procedure.l IsNumberLessThan100_2(n.l)
ProcedureReturn (n<100) Or #False
EndProcedure
Macro Is(expression)
expression Or #False
EndMacro
Dim num.l(1000000) : For ix.l = 0 To 999999 : num(ix) = Random(200) : Next
time.l = ElapsedMilliseconds()
For ix.l = 0 To 999999
a.l = IsNumberLessThan100_0(num(ix))
Next
Debug ElapsedMilliseconds() - time
time = ElapsedMilliseconds()
For ix = 0 To 999999
a = IsNumberLessThan100_1(num(ix))
Next
Debug ElapsedMilliseconds() - time
time = ElapsedMilliseconds()
For ix = 0 To 999999
a = IsNumberLessThan100_2(num(ix))
Next
Debug ElapsedMilliseconds() - time
time = ElapsedMilliseconds()
For ix = 0 To 999999
a = Is(num(ix) < 100)
Next
Debug ElapsedMilliseconds() - time
When you rerun your code sample without debugger and boosting the # of tests 10x you'll see the PB's first example(IsNumberLessThan100_0) is the fastest everytime. It's about twice as fast as Macro Is().
Procedure.l IsNumberLessThan100_0(n.l)
If n<100
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure.l IsNumberLessThan100_1(n.l)
If n<100 : ProcedureReturn #True : EndIf
EndProcedure
Procedure.l IsNumberLessThan100_2(n.l)
ProcedureReturn (n<100) Or #False
EndProcedure
Macro Is(expression)
expression Or #False
EndMacro
Dim num.l(10000000) : For IX.l = 0 To 9999999 : num(IX) = Random(200) : Next
time.l = ElapsedMilliseconds()
For IX.l = 0 To 9999999
a.l = IsNumberLessThan100_0(num(IX))
Next
b.l=ElapsedMilliseconds() - time
MessageRequester("",Str(b))
time = ElapsedMilliseconds()
For IX = 0 To 9999999
a = IsNumberLessThan100_1(num(IX))
Next
b.l=ElapsedMilliseconds() - time
MessageRequester("",Str(b))
time = ElapsedMilliseconds()
For IX = 0 To 9999999
a = IsNumberLessThan100_2(num(IX))
Next
b.l=ElapsedMilliseconds() - time
MessageRequester("",Str(b))
time = ElapsedMilliseconds()
For IX = 0 To 9999999
a = Is(num(IX) < 100)
Next
b.l=ElapsedMilliseconds() - time
MessageRequester("",Str(b))
Demivec wrote:When you rerun your code sample without debugger and boosting the # of tests 10x you'll see the PB's first example(IsNumberLessThan100_0) is the fastest everytime. It's about twice as fast as Macro Is().
Not here.. I get 110, 110, 110, 110.. And sometimes 109 or 111 on some of them..