-
-
Save sklam/11f11a410258ca191e6f263262a4ea65 to your computer and use it in GitHub Desktop.
2x different performance with clang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <time.h> | |
#include <stdio.h> | |
double apple(double *arr, int size) ; | |
double orange(double *arr, int size) ; | |
int main() { | |
const int size = 1000; | |
double arr[size]; | |
for (int i=0; i<size; ++i) { | |
arr[i] = i; | |
} | |
arr[size / 2] = -0.123213; | |
double ra = apple(arr, size); | |
double rb = orange(arr, size); | |
printf("ra = %f | rb = %f\n", ra, rb); | |
// benchmark | |
clock_t ts, te; | |
double dur; | |
const int repeat = 100000; | |
ts = clock(); | |
for (int i=0; i<repeat; ++i) apple(arr, size); | |
te = clock(); | |
dur = te - ts; | |
printf("apple %f\n", dur/CLOCKS_PER_SEC); | |
ts = clock(); | |
for (int i=0; i<repeat; ++i) orange(arr, size); | |
te = clock(); | |
dur = te - ts; | |
printf("orange %f\n", dur/CLOCKS_PER_SEC); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
double apple(double *arr, int size) { | |
double amin = INFINITY; | |
int all_missing = 1; | |
int i; | |
double ai; | |
for (i=0; i<size; ++i) { // increment i here | |
ai = arr[i]; | |
if ( ai <= amin ) { | |
amin = ai; | |
all_missing = 0; | |
} | |
} | |
if (all_missing) { | |
amin = NAN; | |
} | |
return amin; | |
} | |
double orange(double *arr, int size) { | |
double amin = INFINITY; | |
int all_missing = 1; | |
int i; | |
double ai; | |
for (i=0; i<size;) { | |
ai = arr[i]; | |
++i; // increment i here | |
if ( ai <= amin ) { | |
amin = ai; | |
all_missing = 0; | |
} | |
} | |
if (all_missing) { | |
amin = NAN; | |
} | |
return amin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
x86_64 Linux
Looks like the speed impact is even more drastic here, and that it's really not a pure 3.9 issue.