modified WolRons little test code:
For the most part the ABS() and SGN() routines take more time because of the function call...pushing/popping onto the stack and all that...Jeremy's routine works very well, even would work with floats...All I'm doing is a bitwise check of the bit supposedly containing the integer pos/neg flag...it's a really old trick that is only usefull for integers and won't work with floats.
variable% = 1 ;first routine roughly 697 milliseconds ;WolRons routine starttime = MilliSecs() For iter = 1 To 100000000 If Abs(variable) = variable ;variable is positive Else ;variable is negative EndIf Next Print MilliSecs() - starttime ;second routine about 289 milliseconds ;Jeremys routine starttime = MilliSecs() For iter = 1 To 100000000 If variable > 0 ;variable is positive Else ;variable is negative EndIf Next Print MilliSecs() - starttime ;third routine about 756 milliseconds ;Micheals starttime = MilliSecs() For iter = 1 To 100000000 If Sgn(variable) ;variable is positive Else ;variable is negative EndIf Next Print MilliSecs() - starttime ;fourth routine about 253 milliseconds ;Mine ... note: the IF/ELSE results are switched flagger=%10000000000000000000000000000000 starttime = MilliSecs() For iter = 1 To 100000000 If (variable And flagger) ;variable is negative Else ;variable is positive EndIf Next Print MilliSecs() - starttime WaitKey() End
For the most part the ABS() and SGN() routines take more time because of the function call...pushing/popping onto the stack and all that...Jeremy's routine works very well, even would work with floats...All I'm doing is a bitwise check of the bit supposedly containing the integer pos/neg flag...it's a really old trick that is only usefull for integers and won't work with floats.