• 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: A puzzle
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A puzzle


  • Subject: Re: A puzzle
  • From: Philip Aker <email@hidden>
  • Date: Sat, 22 Nov 2008 01:29:36 -0800

On 2008-11-21, at 19:16:16, Mark J. Reed wrote:

Sorry, I get the confusion.  The C code is an exception to the "same
algorithm" bit;  I overstated that aspect.  I only uised the
spit-string-on-1's technique where it didn't require implementing the
basic split-string-on-a-delimiter functionality from scratch. :)


#include <stdio.h>

/*
gcc -O3 -o cio cio.c;time ./cio;
*/

int main( int argc, char *argv[] ) {

	int total = 0;
	int i;
	int j;

	for( i = 0; i < 1000000; i++ ) {
		for( j = i; j; j /= 10 ) {
			if( j % 10 == 1 ) {
				total++;
			}
		}
	}
	printf( "%d\n", total );

	return 0;
}

The technique above is about 18% faster OMM.


Also, is the C code dynamically allocating storage for the result or is it using a pre-defined size?



Allocating storage?  The C code is using nothing but integers.

#include <stdio.h>
int main(int argc, char *argv[])
{
   int total=0;
   int i;
   for (i=0; i<1000000; ++i)
   {
       int j;
       for (j=i; j; j/=10)
           if (1 == j) total++;
   }
   printf("%d\n", total);
   return 0;
}



Philip Aker echo email@hidden@nl | tr a-z@. p-za-o.@

Democracy: Two wolves and a sheep voting on lunch.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
References: 
 >A puzzle (From: Michelle Steiner <email@hidden>)
 >Re: A puzzle (From: Luther Fuller <email@hidden>)
 >Re: A puzzle (From: "Mark J. Reed" <email@hidden>)
 >Re: A puzzle (From: Philip Aker <email@hidden>)
 >Re: A puzzle (From: "Mark J. Reed" <email@hidden>)
 >Re: A puzzle (From: "Mark J. Reed" <email@hidden>)

  • Prev by Date: Re: A puzzle
  • Next by Date: Getting list of items from network slow
  • Previous by thread: Re: A puzzle
  • Next by thread: Re: A puzzle
  • Index(es):
    • Date
    • Thread