Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <inttypes.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define NUM_SAMPLES 500000000
- int randomInt(int min, int max) {
- return rand() % max + min;
- }
- int main() {
- srand(42);
- printf("Starting.\n");
- // Create a large (500M?) array of int16_t numbers to represent sound samples.
- int16_t* samples = calloc(NUM_SAMPLES, sizeof(int16_t));
- for (int i = 0; i < NUM_SAMPLES; i++) {
- samples[i] = randomInt(INT16_MIN, INT16_MAX);
- }
- // Pre-calculate a lookup table (array) of all possible sample
- // values multiplied by the volume factor.
- int16_t* lookup075 = calloc(UINT16_MAX, sizeof(int16_t));
- for (int16_t i = INT16_MIN; i < INT16_MAX; i++) {
- lookup075[(uint16_t)i] = i * 0.75;
- }
- clock_t begin = clock();
- // TEST
- // look up each sample in that table to get the scaled values
- for (int i = 0; i < NUM_SAMPLES; i++) {
- samples[i] = lookup075[(uint16_t)samples[i]];
- }
- // /TEST
- clock_t end = clock();
- // Anti-optimization
- int sum = 0;
- for (int i = 0; i < NUM_SAMPLES; i++) {
- sum += samples[i];
- }
- // /Anti-optimization
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("Sum: %d, Time spent: %.3f\n", sum, time_spent);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement