Skip to content

Instantly share code, notes, and snippets.

@akx
Created September 22, 2022 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akx/9ee93f8a9fdc2cafb3f17b7fd3ecaa55 to your computer and use it in GitHub Desktop.
Save akx/9ee93f8a9fdc2cafb3f17b7fd3ecaa55 to your computer and use it in GitHub Desktop.
$ hyperfine './so73816657-gcc 1 50000000' './so73816657-gcc 2 50000000' './so73816657-clang 1 50000000' './so73816657-clang 2 50000000'
Benchmark 1: ./so73816657-gcc 1 50000000
Time (mean ± σ): 1.703 s ± 0.087 s [User: 1.501 s, System: 0.127 s]
Range (min … max): 1.586 s … 1.861 s 10 runs
Benchmark 2: ./so73816657-gcc 2 50000000
Time (mean ± σ): 1.694 s ± 0.096 s [User: 1.516 s, System: 0.126 s]
Range (min … max): 1.601 s … 1.882 s 10 runs
Benchmark 3: ./so73816657-clang 1 50000000
Time (mean ± σ): 1.618 s ± 0.047 s [User: 1.446 s, System: 0.119 s]
Range (min … max): 1.573 s … 1.725 s 10 runs
Benchmark 4: ./so73816657-clang 2 50000000
Time (mean ± σ): 1.688 s ± 0.098 s [User: 1.512 s, System: 0.123 s]
Range (min … max): 1.586 s … 1.844 s 10 runs
Summary
'./so73816657-clang 1 50000000' ran
1.04 ± 0.07 times faster than './so73816657-clang 2 50000000'
1.05 ± 0.07 times faster than './so73816657-gcc 2 50000000'
1.05 ± 0.06 times faster than './so73816657-gcc 1 50000000'
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <option> <number>\n", argv[0]);
return 1;
}
char option = argv[1][0]; // TODO: error check
int size = atoi(argv[2]); // TODO: error check
srand(time(NULL));
int na = INT_MIN;
int *v_out = (int *)malloc(size * sizeof(int));
int *v_x = (int *)malloc(size * sizeof(int));
// Generate random numbers between 1-3
// 1 -> false
// 2 -> true
// 3 -> na
for (int i = 0; i < size; ++i) {
int elt_out = rand() % 3 + 1;
if (elt_out == 1) {
v_out[i] = 0;
} else if (elt_out == 2) {
v_out[i] = 1;
} else {
v_out[i] = na;
}
int elt_x = rand() % 3 + 1;
if (elt_x == 1) {
v_x[i] = 0;
} else if (elt_x == 2) {
v_x[i] = 1;
} else {
v_x[i] = na;
}
}
if (option == '1') {
// Option 1
for (int i = 0; i < size; ++i) {
int elt_out = v_out[i];
int elt_x = v_x[i];
if (elt_out == 0) {
// Done
} else if (elt_x == 0) {
v_out[i] = 0;
} else if (elt_out == na) {
// Done
} else if (elt_x == na) {
v_out[i] = na;
}
}
}
if (option == '2') {
// Option 2
for (int i = 0; i < size; ++i) {
int elt_out = v_out[i];
if (elt_out == 0) {
continue;
}
int elt_x = v_x[i];
if (elt_x == 0) {
v_out[i] = 0;
} else if (elt_out == na) {
// Done
} else if (elt_x == na) {
v_out[i] = na;
}
}
}
free(v_out);
free(v_x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment