Page 1 of 1

Simple Odd or Even

Posted: Thu Sep 19, 2013 8:54 pm
by minimy
Odd or Even ... that is the question. :mrgreen:
Simple procedure to know if a number is odd.

Code: Select all

Procedure Odd(Number.l)
  ProcedureReturn Mod(Number,2)  
EndProcedure

Debug Odd(7)
Debug Odd(20)

Re: Simple Odd or Even

Posted: Thu Sep 19, 2013 8:59 pm
by luis
Or (without a function call)

Code: Select all

Procedure Odd(n)
  ProcedureReturn n & 1
EndProcedure
Or (without a procedure call)

Code: Select all

Macro ODD (n)
  (n & 1)
EndMacro    

Re: Simple Odd or Even

Posted: Thu Sep 19, 2013 9:52 pm
by minimy
luis wrote:Or (no function call)

Code: Select all

Procedure Odd(Number)
  ProcedureReturn Number & 1
EndProcedure
Hi Luis, thanks for teaching me another way to do it.
I checked the time consumed and are identical.
What is the right way?

Code: Select all

Procedure Odd(Number)
  ProcedureReturn Number & 1
EndProcedure
Procedure TOdd(Number.l)
  ProcedureReturn Mod(Number,2)  
EndProcedure


StartTime = ElapsedMilliseconds()
For p=0 To 100000
  TOdd(p)
Next p
ElapsedTime = ElapsedMilliseconds()-StartTime
Debug "minimy: "+StrU(ElapsedTime)

StartTime = ElapsedMilliseconds()            
For p=0 To 100000
  Odd(p)
Next p
ElapsedTime = ElapsedMilliseconds()-StartTime
Debug "Luis: "+StrU(ElapsedTime)

Re: Simple Odd or Even

Posted: Thu Sep 19, 2013 10:06 pm
by rsts
You can't accurately check execution time when using the debugger.

cheers

Re: Simple Odd or Even

Posted: Thu Sep 19, 2013 10:12 pm
by luis
There is no right way.

Anyway don't do benchmarks with the debugger enabled, it can skew the results. :wink:

Code: Select all

CompilerIf (#PB_Compiler_Debugger = 1)
 CompilerError "Disable Debugger"
CompilerEndIf


Procedure Bitw_Odd(n)
  ProcedureReturn n & 1
EndProcedure

Macro Macro_ODD (n)
  (n & 1)
EndMacro  

Procedure Mod_Odd (n)
  ProcedureReturn Mod(n,2) 
EndProcedure

#times = 500000000

StartTime = ElapsedMilliseconds()
For p=0 To #times
  i = Mod_Odd(p)
Next p
ElapsedTime = ElapsedMilliseconds()-StartTime
MessageRequester("Module: ", StrU(ElapsedTime))

StartTime = ElapsedMilliseconds()           
For p=0 To #times
  i = Bitw_Odd(p)
Next p
ElapsedTime = ElapsedMilliseconds()-StartTime
MessageRequester("bitwise & in proc: ", StrU(ElapsedTime))

StartTime = ElapsedMilliseconds()           
For p=0 To #times
  i = Macro_ODD(p)
Next p
ElapsedTime = ElapsedMilliseconds()-StartTime
MessageRequester("bitwise & in macro :", StrU(ElapsedTime))

Re: Simple Odd or Even

Posted: Thu Sep 19, 2013 10:14 pm
by Tenaja
minimy wrote: What is the right way?
using number & 1 in a macro (instead of a proc) will be the fastest, and most likely a smaller exe size.

Re: Simple Odd or Even

Posted: Thu Sep 19, 2013 10:22 pm
by minimy
Well! Many Thanks! every day I learn something new. :wink:

Re: Simple Odd or Even

Posted: Fri Sep 20, 2013 4:38 am
by Demivec
Tenaja wrote:
minimy wrote: What is the right way?
using number & 1 in a macro (instead of a proc) will be the fastest, and most likely a smaller exe size.
The macro is also be limited to only testing integers. The procedure is better if both integers and floats need to be tested because of the automatic type conversion.