|
|
#include <time.h>double difftime(time2, time1) time_t time2; time_t time1;
See the example which follows.
ANSI X3.159-1989 Programming Language -- C
.
#include <time.h>int mark[10000];
main() { time_t start, finish; register int i, loop, n, num, step; printf("This program takes about 3 minutes "); printf("on an AT and 8 on a PC.\n Working...\n"); time(&start);
for (loop = 0; loop < 1000; ++loop) for (num = 0,n = 3; n < 10000; n += 2) if (!mark[n]) { /* printf("%d\n",n); */ step = 2*n; for (i = 3*n; i < 10000; i += step) mark[i] = -1; ++num; } time(&finish);
/* Prints average of 1000 loops through "sieve": */ printf("\nProgram takes %f seconds to find %d primes.\n", difftime(finish, start), num); }
Output:
Program takes 0.482000 seconds to find 1228 primes.This program calculates the amount of time needed to find the prime numbers between 3 and 10,000. To display the prime numbers, delete the outermost loop and the comment delimiters around the expression
printf("%d",n);
.