qc.hlp (Table of Contents; Topic list)
SIEVE.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* SIEVE.C illustrates timing functions including:
 *      clock           difftime            _bios_timeofday
 *
 * In addition to the timing use shown here, these functions can be
 * used for delay loops as shown for the clock function in BEEP.C.
 */
 
#include <time.h>
#include <stdio.h>
#include <bios.h>
#define TICKPERSEC      18.2
 
int mark[10000];
 
void main()
{
    time_t tstart, tend;    /* For difftime         */
    time_t cstart, cend;    /* For clock            */
    long   bstart, bend;    /* For _bios_timeofday  */
    register int i, loop;
    int n, num, step;
 
    /* Start timing. */
    printf( "Working...\n" );
    time( &tstart );     /* Use time and difftime for timing to seconds   */
    cstart = clock();    /* Use clock for timing to hundredths of seconds */
    _bios_timeofday( _TIME_GETCLOCK, &bstart );
    /* Do timed Sieve of Erotosthenes. */
    for( loop = 0; loop < 250; ++loop)
        for( num = 0, n = 3; n < 10000; n += 2 )
            if( !mark[n] )
            {
                step = 2 * n;
                for (i = 3 * n; i < 10000; i += step)
                    mark[i] = -1;
                ++num;
            }
 
    /* End timing and print results. Note that _bios_timeofday doesn't
     * handle midnight rollover.
     */
    time( &tend );
    printf( "\ndifftime:\t\t%4.2f seconds to find %d primes 50 times\n",
             difftime( tend, tstart ), num );
    cend = clock();
    printf( "\nclock:\t\t\t%4.2f seconds to find %d primes 50 times\n",
            ((float)cend - cstart) / CLK_TCK, num );
    _bios_timeofday( _TIME_GETCLOCK, &bend );
    printf( "\n_bios_timeofday:\t%4.2f seconds to find %d primes 50 times\n",
            ((float)bend - bstart) / TICKPERSEC, num );
}