Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 1.75 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. /*  My results:
  6. @kuma:~/eruta/tool/try$ gcc struct.c -o struct
  7. bjorn@kuma:~/eruta/tool/try$ ./struct
  8. Ticks pass by pointer: 8800000
  9. Result: 1000000002
  10. Ticks pass by value: 5440000
  11. Result: 1000000002
  12. bjorn@kuma:~/eruta/tool/try$ gcc -O struct.c -o struct
  13. bjorn@kuma:~/eruta/tool/try$ ./struct
  14. Ticks pass by pointer: 3360000
  15. Result: 1000000002
  16. Ticks pass by value: 4180000
  17. Result: 1000000002
  18. bjorn@kuma:~/eruta/tool/try$ gcc -O2 struct.c -o struct
  19. bjorn@kuma:~/eruta/tool/try$ ./struct
  20. Ticks pass by pointer: 4590000
  21. Result: 1000000002
  22. Ticks pass by value: 4560000
  23. Result: 1000000002
  24. bjorn@kuma:~/eruta/tool/try$
  25.  
  26. Conclusion: I stand corrected in the case of optimization with gcc.
  27.  
  28. */
  29.  
  30. #ifndef ITERATIONS
  31.   #define ITERATIONS 1000000000
  32. #endif  
  33.  
  34. struct try {
  35.   int one;
  36.   int two;  
  37. };
  38.  
  39. int foo_value(struct try t) {
  40.   return t.one + t.two;
  41. }
  42.  
  43. int foo_pointer(struct try * t) {
  44.   return t->one + t->two;
  45. }
  46.  
  47.  
  48. int main (void) {
  49.   long index;
  50.   clock_t start, stop;        
  51.   struct try   tryv = { 1 , 2};
  52.   struct try * tryp = NULL;
  53.   tryp              = malloc(sizeof(struct try));
  54.   tryp->one         = 1;
  55.   tryp->two         = 2;
  56.   start             = clock();
  57.   for (index = 0 ; index < ITERATIONS ; index++) {
  58.     tryp->two       = foo_pointer(tryp);
  59.   }
  60.   stop              = clock();
  61.   printf("Ticks pass by pointer: %ld\n", stop - start);
  62.   printf("Result: %d\n", tryp->two);
  63.   start             = clock();
  64.   for (index = 0 ; index < ITERATIONS ; index++) {
  65.     tryv.two        = foo_value(tryv);
  66.   }
  67.   stop              = clock();
  68.   printf("Ticks pass by value: %ld\n", stop - start);
  69.   printf("Result: %d\n", tryv.two);
  70.   free(tryp);
  71.   return 0;
  72. }