modernc.org/ccgo/v3@v3.16.14/lib/testdata/CompCert-3.6/test/c/nsieve.c (about)

     1  // The Computer Language Shootout
     2  // http://shootout.alioth.debian.org/
     3  // Precedent C entry modified by bearophile for speed and size, 31 Jan 2006
     4  // Compile with:  -O3 -s -std=c99 -fomit-frame-pointer
     5  
     6  #include <stdio.h>
     7  #include <stdlib.h>
     8  #include <string.h>
     9  
    10  typedef unsigned char boolean;
    11  
    12  
    13  static unsigned int nsieve(int m) {
    14      unsigned int count = 0, i, j;
    15      boolean * flags = (boolean *) malloc(m * sizeof(boolean));
    16      memset(flags, 1, m);
    17  
    18      for (i = 2; i < m; ++i)
    19          if (flags[i]) {
    20              ++count;
    21              for (j = i << 1; j < m; j += i)
    22                  if (flags[j]) flags[j] = 0;
    23      }
    24  
    25      free(flags);
    26      return count;
    27  }
    28  
    29  #define NITER 2
    30  
    31  int main(int argc, char * argv[]) {
    32      int m = argc < 2 ? 9 : atoi(argv[1]);
    33      int i, j;
    34      for (i = 0; i < 3; i++) {
    35        int n = 10000 << (m-i);
    36        unsigned count;
    37        for (j = 0; j < NITER; j++) { count = nsieve(n); }
    38        printf("Primes up to %8d %8u\n", n, count);
    39      }
    40      return 0;
    41  }
    42  
    43  /********
    44   build & benchmark results
    45  
    46  BUILD COMMANDS FOR: nsieve.gcc
    47  
    48  Thu Sep 14 20:51:46 PDT 2006
    49  
    50  /usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -s -std=c99 nsieve.c -o nsieve.gcc_run
    51  
    52  =================================================================
    53  COMMAND LINE (%A is single numeric argument):
    54  
    55  nsieve.gcc_run %A
    56  N=9
    57  
    58  PROGRAM OUTPUT
    59  ==============
    60  Primes up to  5120000   356244
    61  Primes up to  2560000   187134
    62  Primes up to  1280000    98610
    63  *******/