
Untitled
By: a guest on Jan 28th, 2012 | syntax:
None | size: 1.75 KB | hits: 12 | expires: Never
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/* My results:
@kuma:~/eruta/tool/try$ gcc struct.c -o struct
bjorn@kuma:~/eruta/tool/try$ ./struct
Ticks pass by pointer: 8800000
Result: 1000000002
Ticks pass by value: 5440000
Result: 1000000002
bjorn@kuma:~/eruta/tool/try$ gcc -O struct.c -o struct
bjorn@kuma:~/eruta/tool/try$ ./struct
Ticks pass by pointer: 3360000
Result: 1000000002
Ticks pass by value: 4180000
Result: 1000000002
bjorn@kuma:~/eruta/tool/try$ gcc -O2 struct.c -o struct
bjorn@kuma:~/eruta/tool/try$ ./struct
Ticks pass by pointer: 4590000
Result: 1000000002
Ticks pass by value: 4560000
Result: 1000000002
bjorn@kuma:~/eruta/tool/try$
Conclusion: I stand corrected in the case of optimization with gcc.
*/
#ifndef ITERATIONS
#define ITERATIONS 1000000000
#endif
struct try {
int one;
int two;
};
int foo_value(struct try t) {
return t.one + t.two;
}
int foo_pointer(struct try * t) {
return t->one + t->two;
}
int main (void) {
long index;
clock_t start, stop;
struct try tryv = { 1 , 2};
struct try * tryp = NULL;
tryp = malloc(sizeof(struct try));
tryp->one = 1;
tryp->two = 2;
start = clock();
for (index = 0 ; index < ITERATIONS ; index++) {
tryp->two = foo_pointer(tryp);
}
stop = clock();
printf("Ticks pass by pointer: %ld\n", stop - start);
printf("Result: %d\n", tryp->two);
start = clock();
for (index = 0 ; index < ITERATIONS ; index++) {
tryv.two = foo_value(tryv);
}
stop = clock();
printf("Ticks pass by value: %ld\n", stop - start);
printf("Result: %d\n", tryv.two);
free(tryp);
return 0;
}