January 16, 2021, 09:58:19 PM

### Author Topic: isOdd() and isEven()  (Read 176 times)

#### Scaremonger

• Full Member
• Posts: 111
##### isOdd() and isEven()
« on: December 18, 2020, 09:29:34 AM »
After realising that the BMX "mod" expression is rather slow, I re-wrote my isodd() and iseven() functions in C rather than use the internal one.

My previous library functions were:
Code: [Select]
`Function iseven:Int( x:Int ) Return ((x Mod 2)=0)End FunctionFunction isodd:Int( x:Int ) Return Not((x Mod 2)=0)End Function`
This is my C version:
evenodd.c
Code: [Select]
`/* Even and Odd functions  * Si Dunford, 2020 */int iseven( const int n ){ return n % 2 == 0;}int isodd( const int n ){ return n % 2 != 0;}`
Finally; this is my speed test code. I also included a binary AND version that I've used in other languages for comparison:
test_evenodd.bmx
Code: [Select]
`SuperStrict'EVEN and ODD testingImport "evenodd.c"Extern Function iseven:Int( number:Int ) Function isodd:Int( number:Int )EndExternConst REPETITION:Int = 1000000Global start:Int, finish:Intspeedtest( "bmx_even   ", bmx_iseven )speedtest( "bmx_odd    ", bmx_isodd )speedtest( "binaryeven ", binaryeven )speedtest( "binaryodd  ", binaryodd )speedtest( "iseven     ", iseven )speedtest( "isodd      ", isodd )Function speedtest( descr:String, fn:Int( x:Int ) ) start = MilliSecs() For Local n:Int = -REPETITION To REPETITION Local result:Int = fn( n ) Next finish = MilliSecs() Print( descr+": "+(finish-start) )End Function'# Blitzmax isodd() and iseven()'# Prefixed with "bmx_" to differentiate from new C versionFunction bmx_iseven:Int( x:Int ) Return ((x Mod 2)=0)End FunctionFunction bmx_isodd:Int( x:Int ) Return Not((x Mod 2)=0)End Function'# Binary AND versionsFunction binaryeven:Int( x:Int ) Return (x & \$00000001 = 0 ) Return TrueEnd FunctionFunction binaryodd:Int( x:Int ) Return (x & \$00000001 = 1 ) Return TrueEnd Function`
The results improved things quite a bit:
Code: [Select]
`bmx_even   : 246bmx_odd    : 234binaryeven : 238binaryodd  : 253iseven     : 129isodd      : 128`
Regards,
Si...

#### Henri

• Sr. Member
• Posts: 288
##### Re: isOdd() and isEven()
« Reply #1 on: December 18, 2020, 12:09:57 PM »
Hi,

what if you run your test in release mode ?

-Henri
- Got 01100011 problems, but the bit ain't 00000001

#### Derron

• Hero Member
• Posts: 3331
##### Re: isOdd() and isEven()
« Reply #2 on: December 18, 2020, 12:50:35 PM »
https://stackoverflow.com/questions/2229107/what-is-the-fastest-way-to-find-if-a-number-is-even-or-odd

btw you can look into the .bmx folder to check out the generated C code ... I assume some overhead is adding to the bmx time.

bye
Ron

• Hero Member
• Posts: 2057
##### Re: isOdd() and isEven()
« Reply #3 on: December 18, 2020, 01:13:27 PM »

Quote
return (input && 1) =0;

#### Scaremonger

• Full Member
• Posts: 111
##### Re: isOdd() and isEven()
« Reply #4 on: December 18, 2020, 07:03:31 PM »
what if you run your test in release mode ?
Yep. That takes so little time I can't even get a count of it. Good call.
Code: [Select]
`bmx_even   : 0bmx_odd    : 0binaryeven : 0binaryodd  : 0binaryeven2: 0binaryodd2 : 0iseven     : 4isodd      : 4`
https://stackoverflow.com/questions/2229107/what-is-the-fastest-way-to-find-if-a-number-is-even-or-odd
btw you can look into the .bmx folder to check out the generated C code ... I assume some overhead is adding to the bmx time.
Good article, thanks for that. I hadn't thought of looking through the MaxNG C code. Thats a good first stop for future reference.

Quote
return (input && 1) =0;

This is already included as binaryeven() and binaryodd().

#### Matty

• Hero Member
• Posts: 1332
##### Re: isOdd() and isEven()
« Reply #5 on: December 18, 2020, 07:35:26 PM »
What is the project you're working on where optimising this sort of function makes a significant speed boost?  I'm curious because in all my projects optimising is about changing the flow of a program such as reducing sizes of nested for loops etc.
I eat cheese in the trees when its eight degrees

#### Scaremonger

• Full Member
• Posts: 111
##### Re: isOdd() and isEven()
« Reply #6 on: December 18, 2020, 08:56:11 PM »
This came around after I had an issue converting code from python and finding that 'mod' doesn't work the same (see separate thread). It just made me question other places I'd used the expression.