Advertisement
Guest User

w4rt24ffg3g35g

a guest
Oct 22nd, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 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* samples = calloc(NUM_SAMPLES, sizeof(int16_t));
  20.  
  21.     for (int i = 0; i < NUM_SAMPLES; i++) {
  22.         samples[i] = randomInt(INT16_MIN, INT16_MAX);
  23.     }
  24.  
  25.     // Pre-calculate a lookup table (array) of all possible sample
  26.     // values multiplied by the volume factor.
  27.     int16_t* lookup075 = calloc(UINT16_MAX, sizeof(int16_t));
  28.     for (int16_t i = INT16_MIN; i < INT16_MAX; i++) {
  29.         lookup075[(uint16_t)i] = i * 0.75;
  30.     }
  31.  
  32.     clock_t begin = clock();
  33.  
  34.     // TEST
  35.     // look up each sample in that table to get the scaled values
  36.     for (int i = 0; i < NUM_SAMPLES; i++) {
  37.         samples[i] = lookup075[(uint16_t)samples[i]];
  38.     }
  39.     // /TEST
  40.  
  41.     clock_t end = clock();
  42.  
  43.     // Anti-optimization
  44.     int sum = 0;
  45.     for (int i = 0; i < NUM_SAMPLES; i++) {
  46.         sum += samples[i];
  47.     }
  48.     // /Anti-optimization
  49.  
  50.     double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  51.  
  52.     printf("Sum: %d, Time spent: %.3f\n", sum, time_spent);
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement