Advertisement
Guest User

wegwfg2f2t

a guest
Oct 21st, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <inttypes.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define NUM_SAMPLES 500000000
  8.  
  9. int randomInt(int min, int max) {
  10.     return rand() % max + min;
  11. }
  12.  
  13. int main() {
  14.     srand(42);
  15.  
  16.     printf("Starting.\n");
  17.  
  18.     // Create a large (500M?) array of int16_t numbers to represent sound samples.
  19.     int16_t* nums = calloc(NUM_SAMPLES, 2);
  20.  
  21.     for (int i = 0; i < NUM_SAMPLES; i++) {
  22.         nums[i] = randomInt(INT16_MIN, INT16_MAX);
  23.     }
  24.  
  25.     clock_t begin = clock();
  26.  
  27.     // TEST
  28.  
  29.     // Scale each sample by the volume factor (0.75). Store the results into the original array or into a separate result array.
  30.     // Convert the volume factor 0.75 to a fix-point integer by multiplying by a binary number representing a fixed-point value "1".
  31.     for (int i = 0; i < NUM_SAMPLES; i++) {
  32.         int16_t volumeInt = 0.75 * 0b100000000;
  33.         int16_t shifted = volumeInt >>8;
  34.         nums[i] = nums[i] * shifted;
  35.     }
  36.  
  37.     // /TEST
  38.  
  39.     clock_t end = clock();
  40.  
  41.     // Anti-optimization
  42.     int sum = 0;
  43.     for (int i = 0; i < NUM_SAMPLES; i++) {
  44.         sum += nums[i];
  45.     }
  46.     // /Anti-optimization
  47.  
  48.     double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  49.  
  50.     printf("Sum: %d, Time spent: %.3f\n", sum, time_spent);
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement