• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Stupid newbie question - determine if integer is even or odd
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stupid newbie question - determine if integer is even or odd


  • Subject: Re: Stupid newbie question - determine if integer is even or odd
  • From: John Haager <email@hidden>
  • Date: Tue, 13 Aug 2002 17:37:00 -0700

From: John Haager <email@hidden>
Date: Tue Aug 13, 2002 05:35:41 PM US/Pacific
To: Steve Bird <email@hidden>
Subject: Re: Stupid newbie question - determine if integer is even or odd

On Tuesday, August 13, 2002, at 04:28 PM, Steve Bird wrote:

<Obsessed cycle-counter>
I'd just like to point out that even though everyone else who answered this question did a mod , I did not see a single answer of
if (myVal & 0x1) {
}

--- ANDing is most often faster than division. It pains me to see a division whose result (quotient) is discarded. That's just cruel. There IS such a thing as the Society for the Prevention of Cruelty to Computers,
you know.

And just to prove that to myself (after I suggested it), I wrote a quick test program which ran each test 100 million times. Results are as follows:

Modulus:
Elapsed Time: 3.054537
Ops/Sec: 32738185.232402

Bitwise And:
Elapsed Time: 2.382001
Ops/Sec: 41981511.857466

These were using inlined operations as the overhead added by a function call pretty much erases any benefit of using bitwise comparisons. Using function calls yielded elapsed times of 8.2 and 8.1 respectively. Really kills performance.

#include <iostream.h>
#include <sys/time.h>

#define floattime(x) ( x.tv_sec + ( x.tv_usec / 1000000.0 ) )
#define modulo(value) ( value % 2 == 0 )
#define bitwise(value) ( value & 1 )

void main ( int argc, char ** argv )
{
int i = 0 ;
int limit = 100000000 ;
bool res ;
struct timeval now ;
struct timeval then ;

gettimeofday ( &now, NULL ) ;
printf ( "%0.6f\n", floattime(now) ) ;
then = now ;

for ( i = 0 ; i < limit ; i++ )
{
res = modulo ( i ) ;
}

gettimeofday ( &now, NULL ) ;
printf ( "%0.6f\n", floattime(now) ) ;
printf ( "Elapsed Time: %0.6f\n", floattime(now) - floattime(then) ) ;
printf ( "Ops/Sec: %0.6f\n", limit/(floattime(now)-floattime(then))) ;
gettimeofday ( &then, NULL ) ;

for ( i = 0 ; i < limit ; i++ )
{
res = bitwise ( i ) ;
}

gettimeofday ( &now, NULL ) ;
printf ( "%0.6f\n", floattime(now) ) ;
printf ( "Elapsed Time: %0.6f\n", floattime(now) - floattime(then) ) ;
printf ( "Ops/Sec: %0.6f\n", limit/(floattime(now)-floattime(then))) ;
}

-> John Haager <-
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Stupid newbie question - determine if integer is even or odd (From: Steve Bird <email@hidden>)

  • Prev by Date: Re: Stupid newbie question - determine if integer is even or odd
  • Next by Date: Re: Stupid newbie question - determine if integer is even or odd
  • Previous by thread: Re: Stupid newbie question - determine if integer is even or odd
  • Next by thread: Re: Stupid_newbie_question_determine_if_integer is even or odd
  • Index(es):
    • Date
    • Thread