Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int *low_address;
  5. int *high_address;
  6.  
  7. int iterative_fibonacci(int n)
  8. {
  9.     if (&n < low_address) low_address = &n;  // update the lowest memory address reached.
  10.     int t1 = 0, t2 = 1, nextTerm;
  11.     for(int i= 1; i<n;i++)
  12.     {  
  13.         nextTerm = t1 + t2;
  14.         t1 = t2;
  15.         t2 = nextTerm;
  16.     }
  17.     return t2;
  18. }
  19.  
  20. int recursive_fibonacci(int n) {
  21.     if (&n < low_address) low_address = &n;  // update the lowest memory address reached.
  22.     if (n == 0)
  23.     {
  24.         return 0;
  25.     }
  26.     if (n == 1)
  27.     {
  28.         return 1;
  29.     }
  30.  
  31.     else{
  32.         return (recursive_fibonacci(n-1) + recursive_fibonacci(n-2));
  33.     }
  34. }
  35.  
  36. int main(){
  37.     int i=0, n = 40;
  38.     clock_t t1, t2;
  39.     int m1, m2;
  40.     double time_span1, time_span2;
  41.    
  42.     printf("Iterative algorithm measurement:\n");
  43.    
  44.     //measuring memory span for iterative_fibonacci function
  45.     high_address = &i;
  46.     low_address = high_address;
  47.     printf("iterative_fibonacci(%d): %d\n", n, iterative_fibonacci(n));
  48.     printf("high address: %lu\n", (high_address));
  49.     printf("low address: %lu\n", (low_address));
  50.     int memory_span1 = (unsigned long) high_address - (unsigned long) low_address;
  51.     printf("memory span: %d\n",memory_span1);
  52.  
  53.     //measuring runtime for iterative_fibonacci function
  54.     m1 = 500000;
  55.     t1=clock();
  56.     for (i=0; i< m1; i++) {
  57.         iterative_fibonacci(n);
  58.     }
  59.     t2=clock();
  60.     time_span1 = (double) t2-t1;
  61.     printf("time_span(iterative_fibonacci(%d) for %d times): %0.1f (ms)\n", n, m1, time_span1);
  62.    
  63.     printf("\nRecursive algorithm measurement:\n");
  64.    
  65.     //memory span and time span measuring for the recursive_fibonacci function
  66.     high_address = &i;
  67.     low_address = high_address;
  68.     printf("recursive_fibonacci(%d): %d\n", n, recursive_fibonacci(n));
  69.     printf("high address: %lu\n", high_address);
  70.     printf("low address: %lu\n", low_address);
  71.     int memory_span2 = (unsigned long) high_address - (unsigned long) low_address;
  72.     printf("memory span: %d\n",memory_span1);
  73.    
  74.    
  75.     // comparison
  76.     printf("\nComparison of recursive and iterative algorithms:\n");
  77.     printf("memory_span(recursive_fibonacci(%d))/memory_span(iterative_fibonacci(%d)): %0.1f\n", n, n, ((double) memory_span2)/memory_span1);
  78.     printf("time_span(recursive_fibonacci(%d))/time_span(iterative_fibonacci(%d)): %0.1f\n", n, n, (time_span2/time_span1)*(m1/m2));
  79.    
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement