Skip to content

Instantly share code, notes, and snippets.

@sklam

sklam/main.c Secret

Created December 22, 2016 23: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 sklam/11f11a410258ca191e6f263262a4ea65 to your computer and use it in GitHub Desktop.
Save sklam/11f11a410258ca191e6f263262a4ea65 to your computer and use it in GitHub Desktop.
2x different performance with clang
#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;
}
#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;
}
@ml31415
Copy link

ml31415 commented Feb 15, 2017

michael@nyx:~/w/apples# ./test-3.8 
ra = -0.123213 | rb = -0.123213
apple 0.242737
orange 0.053176
michael@nyx:~/w/apples# ./test-3.6
ra = -0.123213 | rb = -0.123213
apple 0.235650
orange 0.054918

x86_64 Linux
Looks like the speed impact is even more drastic here, and that it's really not a pure 3.9 issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment