lisp - Scheme Code Analysis for Space vs. Time -


i'm working way through online mit lectures classic 6.001 course: structure , interpretation of computer programs.

i'm trying gain understanding of analyzing code complexity in terms of memory usage vs. execution time. in first few lectures, present solution in scheme fibonacci series.

the solution present in videos 1 has characteristic of growing in memory space x (linear recursion performance), presents big problem fibonacci series. try find larger fibonacci number, space required recursion becomes huge.

they suggest trying find way linear iteration performance, memory space needed remains constant throughout computation , doesn't depend on x.

my solution below. specific question is, performance analysis of scheme code below, in terms of memory usage vs. execution time?

(define (fib x)    (define (fib-helper target total current-index i-2 i-1)      (if (> current-index target)             (if (= target 1)                 0                 total)             (fib-helper target                         (+ i-2 i-1)                         (+ current-index 1)                         i-1                         (+ i-2 i-1))))    (fib-helper x 1 3 0 1)) 

well, considering (fib n) causes n-1 calls fib-helper, solution runs in linear time. fib-helper calls once each iteration, , each iteration tail call, program runs in constant space.

this means call (fib 1000) should take ten times cpu time of (fib 100) while taking same amount of memory.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -