DEV Community

Smit Gabani
Smit Gabani

Posted on

SPO600 Lab 5 - Algorithm Selection Part 2: In depth

We want to measure the performance of each algorithm specifically and nothing else should be in the way in order to give a correct measurement of the time elapsed performing the algorithm.

In order to do that we need to make some modification to all the files.

I have the code here for vol0.c

// vol0.c - naive scaling algorithm
// Chris Tyler 2017.11.29-2021.11.16 - Licensed under GPLv3.
// For the Seneca College SPO600 Course

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "vol.h"
#include <time.h>

int16_t scale_sample(int16_t sample, int volume) {

        return (int16_t) ((float) (volume/100.0) * (float) sample);
}

int main() {
        int             x;
        int             ttl=0;

// ---- Create in[] and out[] arrays
        int16_t*        in;
        int16_t*        out;
        in=(int16_t*) calloc(SAMPLES, sizeof(int16_t));
        out=(int16_t*) calloc(SAMPLES, sizeof(int16_t));

        clock_t start_t, end_t;
        start_t = clock();
// ---- Create dummy samples in in[]
        vol_createsample(in, SAMPLES);

// ---- This is the part we're interested in!
// ---- Scale the samples from in[], placing results in out[]
//
        for (x = 0; x < SAMPLES; x++) {
                out[x]=scale_sample(in[x], VOLUME);
        }
        end_t = clock();
        printf("Time elapsed: %f\n", ((double)start_t - end_t)/CLOCKS_PER_SEC);

// ---- This part sums the samples. (Why is this needed?)
        for (x = 0; x < SAMPLES; x++) {
                ttl=(ttl+out[x])%1000;
        }

// ---- Print the sum of the samples. (Why is this needed?)
        printf("Result: %d\n", ttl);

        return 0;
}
Enter fullscreen mode Exit fullscreen mode

Declare start_t and end_t which are of type clock_t.

We wrap the scaleing part in start_t = clock(); and end_t = clock();.

And then print the time
printf("Time elapsed: %f\n", ((double)start_t - end_t)/CLOCKS_PER_SEC);.

DO THIS FOR ALL THE vol*.c FILES.

When you run the ./vol0 you will get such output.

Image description

Run the ./vol0 in a loop:

Image description

Observations:

AArch64 system:
vol0 vol1 vol2 vol4 vol5
1 4.980222 4.313869 10.567804 3.481179 4.04965
2 4.968114 4.323427 10.560218 3.456832 4.045124
3 4.959307 4.30239 10.59247 3.460554 4.045296
4 4.973315 4.325246 10.590718 3.469896 4.028074
5 4.961954 4.31275 10.585899 3.493608 4.039965
6 4.976137 4.320063 10.532851 3.438831 4.040304
7 4.959057 4.349743 10.642468 3.482111 4.055758
8 4.960718 4.317437 10.534451 3.469382 4.150955
9 4.95485 4.336698 10.548297 3.451517 4.085376
10 4.960642 4.329521 10.552774 3.455712 4.022335
11 4.952321 4.332141 10.550225 3.384459 4.041784
12 4.990002 4.334904 10.572559 3.423148 4.058293
13 4.942643 4.326342 10.545258 3.420771 4.096189
14 4.957297 4.317604 10.548684 3.391878 4.020974
15 4.967439 4.317819 10.558838 3.433111 4.062904
average 4.964267867 4.323996933 10.5655676 3.4475326 4.056198733
median 4.960718 4.323427 10.558838 3.455712 4.045296

Top comments (0)