X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=samples%2Fsieve.c;h=8d0619888235d9c2e3549919b584474be4496501;hb=df80d071e8eeb799bd1d8ec55acd1ee92714c752;hp=cb6783c935ad4e997fdb76c928301b7c439a1451;hpb=fc6c0a461581207ab296e76d56913b58c42c1f75;p=cc65 diff --git a/samples/sieve.c b/samples/sieve.c index cb6783c93..8d0619888 100644 --- a/samples/sieve.c +++ b/samples/sieve.c @@ -1,6 +1,6 @@ /* - * Calculate all primes up to a specific number. - */ +** Calculate all primes up to a specific number. +*/ @@ -11,27 +11,42 @@ #include +/* Workaround missing clock stuff */ +#ifdef __APPLE2__ +# define clock() 0 +# define CLOCKS_PER_SEC 1 +#endif + + /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ -#define COUNT 8192 /* Up to what number? */ -#define SQRT_COUNT 91 /* Sqrt of COUNT */ +#define COUNT 16384 /* Up to what number? */ +#define SQRT_COUNT 128 /* Sqrt of COUNT */ static unsigned char Sieve[COUNT]; /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ -#pragma staticlocals(1); +#pragma static-locals(1); + + + +static char ReadUpperKey (void) +/* Read a key from console, convert to upper case and return */ +{ + return toupper (cgetc ()); +} @@ -39,11 +54,13 @@ int main (void) { /* Clock variable */ clock_t Ticks; + unsigned Sec; + unsigned Milli; /* This is an example where register variables make sense */ register unsigned char* S; - register unsigned I; - register unsigned J; + register unsigned I; + register unsigned J; /* Output a header */ printf ("Sieve benchmark - calculating primes\n"); @@ -56,36 +73,47 @@ int main (void) /* Execute the sieve */ I = 2; while (I < SQRT_COUNT) { - if (Sieve[I] == 0) { - /* Prime number - mark multiples */ - S = &Sieve[J = I*2]; - while (J < COUNT) { - *S = 1; - S += I; - J += I; - } - } - ++I; + if (Sieve[I] == 0) { + /* Prime number - mark multiples */ + J = I*2; + S = &Sieve[J]; + while (J < COUNT) { + *S = 1; + S += I; + J += I; + } + } + ++I; } /* Calculate the time used */ Ticks = clock() - Ticks; + Sec = (unsigned) (Ticks / CLOCKS_PER_SEC); + Milli = ((Ticks % CLOCKS_PER_SEC) * 1000) / CLOCKS_PER_SEC; /* Print the time used */ - printf ("Time used: %lu ticks\n", Ticks); - printf ("Press Q to quit, any other key for list\n"); + printf ("Time used: %u.%03u seconds\n", Sec, Milli); + printf ("Q to quit, any other key for list\n"); /* Wait for a key and print the list if not 'Q' */ - if (toupper (cgetc()) != 'Q') { - /* Print the result */ - for (I = 2; I < COUNT; ++I) { - if (Sieve[I] == 0) { - printf ("%4d\n", I); - } - if (kbhit() && toupper (cgetc()) == 'Q') { - break; - } - } + if (ReadUpperKey () != 'Q') { + /* Print the result */ + J = 0; + for (I = 2; I < COUNT; ++I) { + if (Sieve[I] == 0) { + printf ("%4d\n", I); + if (++J == 23) { + printf ("Q to quit, any other key continues\n"); + if (ReadUpperKey () == 'Q') { + break; + } + J = 0; + } + } + if (kbhit() && ReadUpperKey () == 'Q') { + break; + } + } } return EXIT_SUCCESS;