Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : cp264a2_jugb6050.c
  4. Author : Raiyan Jugbhery
  5. Version :
  6. Copyright : Your copyright notice
  7. ============================================================================
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <time.h>
  12.  
  13. int *la;
  14. int *ha;
  15.  
  16. int iterative_fibonacci(int n)
  17. {
  18. if (&n < la) la = &n; // update the lowest memory address reached.
  19.  
  20. int f[n+2]; // initializes to make sure we can handle f[0] and f[1]
  21. int i;
  22.  
  23. f[0] = 0; // sets base case for index 0.
  24. f[1] = 1; // sets base case for index 1.
  25.  
  26. for (i = 2; i <= n; i++) // loop to generate fibonacci numbers until f[n]
  27. {
  28. f[i] = f[i-1] + f[i-2];
  29. }
  30. return f[n];
  31.  
  32. }
  33.  
  34. int recursive_fibonacci(int n) {
  35. if (&n < la) la = &n; // update the lowest memory address reached.
  36.  
  37. if (n <= 1){
  38. return n;
  39. }
  40. else{
  41. return recursive_fibonacci(n-1) + recursive_fibonacci(n-2);
  42. }
  43.  
  44. }
  45.  
  46. int main(){
  47. int i=0, n = 40;
  48. clock_t t1, t2;
  49. int m1, m2;
  50. double time_span1, time_span2;
  51.  
  52. printf("Iterative algorithm measurement:\n");
  53.  
  54. //measuring memory span for iterative_fibonacci function
  55. ha = &i;
  56. la = ha;
  57. printf("iterative_fibonacci(%d): %d\n", n, iterative_fibonacci(n));
  58. printf("high address: %lu\n", ha);
  59. printf("low address: %lu\n", la);
  60. int memory_span1 = (unsigned long) ha - (unsigned long) la;
  61. printf("memory span: %d\n",memory_span1);
  62.  
  63. //measuring runtime for iterative_fibonacci function
  64. m1 = 500000;
  65. t1=clock();
  66. for (i=0; i< m1; i++) {
  67. iterative_fibonacci(n);
  68. }
  69. t2=clock();
  70. time_span1 = (double) t2-t1;
  71. printf("time_span(iterative_fibonacci(%d) for %d times): %0.1f (ms)\n", n, m1, time_span1);
  72.  
  73. printf("\nRecursive algorithm measurement:\n");
  74.  
  75. //--------------------------------------------------------------------------------------------
  76.  
  77. // add your memory span and time span measuring for the recursive_fibonacci function
  78.  
  79. //measuring memory span for recursive_fibonacci function
  80. ha = &i;
  81. la = ha;
  82. printf("iterative_fibonacci(%d): %d\n", n, recursive_fibonacci(n));
  83. printf("high address: %lu\n", ha);
  84. printf("low address: %lu\n", la);
  85. int memory_span2 = (unsigned long) ha - (unsigned long) la;
  86. printf("memory span: %d\n",memory_span2);
  87.  
  88. //measuring runtime for recursive_fibonacci function
  89. m2 = 10;
  90. t2=clock();
  91. for (i=0; i< m2; i++) {
  92. recursive_fibonacci(n);
  93. }
  94. t2=clock();
  95. time_span2 = (double) t2-t1;
  96. printf("time_span(recursive_fibonacci(%d) for %d times): %0.1f (ms)\n", n, m2, time_span2);
  97.  
  98.  
  99.  
  100. //---------------------------------------------------------------------------------------------
  101.  
  102. // comparison
  103. printf("\nComparison of recursive and iterative algorithms:\n");
  104. printf("memory_span(recursive_fibonacci(%d))/memory_span(iterative_fibonacci(%d)): %0.1f\n", n, n, ((double) memory_span2)/memory_span1);
  105. printf("time_span(recursive_fibonacci(%d))/time_span(iterative_fibonacci(%d)): %0.1f\n", n, n, (time_span2/time_span1)*(m1/m2));
  106.  
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement