Advertisement
Guest User

fwf23r2fr2t

a guest
Oct 22nd, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 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, 2);
  20.  
  21.     for (int i = 0; i < NUM_SAMPLES; i++) {
  22.         samples[i] = randomInt(INT16_MIN, INT16_MAX);
  23.     }
  24.  
  25.     clock_t begin = clock();
  26.  
  27.     // TEST
  28.     for (int i = 0; i < NUM_SAMPLES; i++) {
  29.         samples[i] = samples[i] * 0.75;
  30.     }
  31.     // /TEST
  32.  
  33.     clock_t end = clock();
  34.  
  35.     // Anti-optimization
  36.     int sum = 0;
  37.     for (int i = 0; i < NUM_SAMPLES; i++) {
  38.         sum += samples[i];
  39.     }
  40.     // /Anti-optimization
  41.  
  42.     double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  43.  
  44.     printf("Sum: %d, Time spent: %.3f\n", sum, time_spent);
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement