APPFS
Advanced practical programming for scientists
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
ex4_cond.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 
5 #define SIZE 100000000
6 
7 #define GET_SEC(a, b) ((b - a) / (double)CLOCKS_PER_SEC)
8 
9 static int cmp_dbl(const void* a, const void* b)
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 }
20 
21 int main(int argc, char** argv)
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 main(int argc, char **argv)
Definition: ex4_cond.c:21
int i
Definition: ex4_struct.c:4
#define GET_SEC(a, b)
Definition: ex4_cond.c:7