Advertisement
programmer1997

P2.c

Nov 22nd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. void main(){
  7.  
  8.     int16_t *arr, *lookup;
  9.     long long int sum = 0, value = 0, count = 0, size = 500000000;
  10.  
  11.     arr = (int16_t*) calloc(size, sizeof(int16_t));
  12.     lookup = (int16_t*) calloc(65535, sizeof(int16_t));
  13.  
  14.     srand(1);
  15.  
  16.     for(count = 0; count < size; count++){
  17.         arr[count] = (rand() % 65537) - 32769;
  18.     }
  19.  
  20.     for(count = 0; count < 65536; count++){
  21.         value = count; 
  22.         lookup[count] = (value - 32768)  * 0.75;
  23.     }
  24.  
  25.     clock_t begin = clock();
  26.  
  27.     for(count = 0; count < size; count++){
  28.             value = arr[count];
  29.         arr[count] = lookup[value + 32768];
  30.     }  
  31.  
  32.     clock_t end = clock();
  33.     double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  34.  
  35.     for(count = 0; count < size; count++){  
  36.         sum += arr[count];
  37.     }
  38.  
  39.     printf("sum is %d\n", sum);
  40.     printf("time taken for part b was %lf\n", time_spent);
  41.  
  42.     free(arr);
  43.     free(lookup);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement