Simple Odd or Even

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 634
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Simple Odd or Even

Post 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)
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Simple Odd or Even

Post 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    
"Have you tried turning it off and on again ?"
User avatar
minimy
Enthusiast
Enthusiast
Posts: 634
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple Odd or Even

Post 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)
If translation=Error: reply="Sorry, Im Spanish": Endif
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Simple Odd or Even

Post by rsts »

You can't accurately check execution time when using the debugger.

cheers
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Simple Odd or Even

Post 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))
"Have you tried turning it off and on again ?"
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Simple Odd or Even

Post 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.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 634
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple Odd or Even

Post by minimy »

Well! Many Thanks! every day I learn something new. :wink:
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Simple Odd or Even

Post 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.
Post Reply