APPFS
Advanced practical programming for scientists
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions
ex4_cond.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

Go to the source code of this file.

Macros

#define SIZE   100000000
 
#define GET_SEC(a, b)   ((b - a) / (double)CLOCKS_PER_SEC)
 

Functions

static int cmp_dbl (const void *a, const void *b)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

#define GET_SEC (   a,
 
)    ((b - a) / (double)CLOCKS_PER_SEC)

Definition at line 7 of file ex4_cond.c.

#define SIZE   100000000

Definition at line 5 of file ex4_cond.c.

Function Documentation

static int cmp_dbl ( const void *  a,
const void *  b 
)
static

Definition at line 9 of file ex4_cond.c.

10 {
11  const double* aa = a;
12  const double* bb = b;
13 
14  if (aa < bb)
15  return -1;
16  if (aa > bb)
17  return 1;
18  return 0;
19 }
int main ( int  argc,
char **  argv 
)

Definition at line 21 of file ex4_cond.c.

22 {
23  clock_t start;
24  int* x = malloc(SIZE * sizeof(*x));
25  double sum;
26  int q;
27 
28  for(int i = 0; i < SIZE; i++)
29  x[i] = (rand() % 256);
30 
31  sum = 0.0; start = clock();
32 
33  /* In order 1 */
34  for(int i = 0; i < SIZE; i++)
35  sum += x[i];
36 
37  printf("IO time=%.3f sum=%.1f\n", GET_SEC(start, clock()), sum);
38 
39  sum = 0.0; start = clock(); q = 0;
40 
41  /* In order 1 */
42  for(int i = 0; i < SIZE; i++)
43  if (x[i] <= 128)
44  sum += x[i];
45  else
46  q++;
47 
48  printf("IO time=%.3f sum=%.1f %d\n", GET_SEC(start, clock()), sum, q);
49 
50  qsort(x, SIZE, sizeof(*x), cmp_dbl);
51 
52  sum = 0.0; start = clock(); q = 0;
53 
54  /* In order 1 */
55  for(int i = 0; i < SIZE; i++)
56  if (x[i] <= 128)
57  sum += x[i];
58  else
59  q++;
60 
61  printf("IO time=%.3f sum=%.1f %d\n", GET_SEC(start, clock()), sum, q);
62 
63 }
#define malloc(a)
Definition: mshell.h:53
char x
Definition: ex4_struct.c:4
static int cmp_dbl(const void *a, const void *b)
Definition: ex4_cond.c:9
#define SIZE
Definition: ex4_cond.c:5
int i
Definition: ex4_struct.c:4
#define GET_SEC(a, b)
Definition: ex4_cond.c:7