Advertisement
Guest User

WTFC

a guest
Jun 29th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5.  
  6. #define ASIZE 100000
  7. #define ITERS 5000
  8.  
  9. int prdel[ASIZE];
  10.  
  11. double get_time(void) {
  12.     struct timeval tv;
  13.  
  14.     gettimeofday(&tv, NULL);
  15.     return (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;
  16. }
  17.  
  18. int cmp_ints(const void * a, const void * b) {
  19.     return *(int*)a<*(int*)b ? -1 : (*(int*)a==*(int *)b ? 0 : 1);
  20. }
  21.  
  22.  
  23. int main(int argc, char * argv[]) {
  24.  
  25.  
  26.     int i,j;
  27.     double start, finish;
  28.     long sum = 0;
  29.  
  30.     srand(1337);
  31.  
  32.     for (i = 0; i < ASIZE; i++) {
  33.         prdel[i] = rand();
  34.     }
  35.  
  36.     // Fill array with random numbers
  37.     for (i = 0; i < ASIZE; i++) {
  38.         prdel[i] = rand();
  39.     }
  40.  
  41.     start = get_time();
  42.  
  43.     // If WTF specified on the cmdline, sort the array
  44.     if (argc == 2 && !strcmp(argv[1], "WTF")) {
  45.         qsort(prdel, ASIZE, sizeof(int), cmp_ints);
  46.     }
  47.  
  48.     // calculate sum of all "large" numbers in the array, repeat a couple of times.
  49.     for(j = 0; j < ITERS; j++) {
  50.         for(i = 0; i < ASIZE; i++) {
  51.             if (prdel[i] >= RAND_MAX/2) {
  52.                 sum += prdel[i];
  53.             }
  54.         }
  55.     }
  56.     finish = get_time();
  57.  
  58.     // print result and time taken
  59.     printf("Sum: %ld\nTime: %g\n", sum, finish - start);
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement