Skip to content

Instantly share code, notes, and snippets.

@aiht
Last active August 29, 2015 13:58
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 aiht/9971486 to your computer and use it in GitHub Desktop.
Save aiht/9971486 to your computer and use it in GitHub Desktop.
// Simple function parameter passing style speed test.
// Compiled with
// gcc -O2 -DBUILD_FUNCS -Wall bench.c -c -o funcs.o; gcc -O2 -Wall bench.c funcs.o -o bench
// Some notes:
// Split into two modules to allow realistic -O2 without the compiler being able to avoid doing any work at all.
// Includes the variable argc input and returns the sum for the same reason.
// f_no_params is used as a control for loop overhead.
#include <time.h>
#include <stdio.h>
typedef struct {
int p0;
int p1;
int p2;
int p3;
int p4;
int p5;
int p6;
int p7;
int p8;
int p9;
} params;
int f_no_params();
int f_struct_ordered(params *p);
int f_struct_unordered(params *p);
int f_struct_ordered_byval(params p);
int f_struct_unordered_byval(params p);
int f_params_ordered(
int p0,
int p1,
int p2,
int p3,
int p4,
int p5,
int p6,
int p7,
int p8,
int p9
);
int f_params_unordered(
int p0,
int p1,
int p2,
int p3,
int p4,
int p5,
int p6,
int p7,
int p8,
int p9
);
#ifdef BUILD_FUNCS
int f_no_params() {
return (int)&f_no_params;
}
int f_struct_ordered(params *p) {
return p->p0 ^ p->p1 ^ p->p2 ^ p->p3 ^ p->p4 ^ p->p5 ^ p->p6 ^ p->p7 ^ p->p8 ^ p->p9;
}
int f_struct_unordered(params *p) {
return p->p1 ^ p->p7 ^ p->p0 ^ p->p5 ^ p->p2 ^ p->p3 ^ p->p4 ^ p->p9 ^ p->p6 ^ p->p8;
}
int f_struct_ordered_byval(params p) {
return p.p0 ^ p.p1 ^ p.p2 ^ p.p3 ^ p.p4 ^ p.p5 ^ p.p6 ^ p.p7 ^ p.p8 ^ p.p9;
}
int f_struct_unordered_byval(params p) {
return p.p1 ^ p.p7 ^ p.p0 ^ p.p5 ^ p.p2 ^ p.p3 ^ p.p4 ^ p.p9 ^ p.p6 ^ p.p8;
}
int f_params_ordered(
int p0,
int p1,
int p2,
int p3,
int p4,
int p5,
int p6,
int p7,
int p8,
int p9
) {
return p0 ^ p1 ^ p2 ^ p3 ^ p4 ^ p5 ^ p6 ^ p7 ^ p8 ^ p9;
}
int f_params_unordered(
int p0,
int p1,
int p2,
int p3,
int p4,
int p5,
int p6,
int p7,
int p8,
int p9
) {
return p1 ^ p7 ^ p0 ^ p5 ^ p2 ^ p3 ^ p4 ^ p9 ^ p6 ^ p8;
}
#else // BUILD_FUNCS
int main(int argc, char** argv) {
static const long long ITERATIONS = 20000000000;
long long i, val = 0;
params p = {0,1,2,3,4,5,6,7,argc,9};
clock_t tm1 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_no_params();
}
clock_t t0 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_struct_ordered(&p);
}
clock_t t1 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_struct_unordered(&p);
}
clock_t t2 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_struct_ordered_byval(p);
}
clock_t t3 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_struct_unordered_byval(p);
}
clock_t t4 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_params_ordered(0,1,2,3,4,5,6,7,argc,9);
}
clock_t t5 = clock();
for(i=0;i<ITERATIONS;i++) {
val += f_params_unordered(0,1,2,3,4,5,6,7,argc,9);
}
clock_t t6 = clock();
fprintf(stderr, "%f,%f,%f,%f,%f,%f,%f\n",
((double)t0-tm1)/CLOCKS_PER_SEC,
((double)t1-t0)/CLOCKS_PER_SEC,
((double)t2-t1)/CLOCKS_PER_SEC,
((double)t3-t2)/CLOCKS_PER_SEC,
((double)t4-t3)/CLOCKS_PER_SEC,
((double)t5-t4)/CLOCKS_PER_SEC,
((double)t6-t5)/CLOCKS_PER_SEC);
return val;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment