Author | Message |
---|---|
rabbott
Posts: 1649
|
Posted 12:16 Feb 18, 2019 |
If you are interested, here's my Haskell version of top_down_iter. data FibElement = I Integer | Fib Integer | Plus deriving Show fib_top_down_iter_v2 :: Integer -> Integer -- The first list is reversed from the Python version. |
rabbott
Posts: 1649
|
Posted 16:39 Feb 18, 2019 |
This version includes an optional trace. It also drops the
Last edited by rabbott at
19:33 Feb 18, 2019.
|
jungsoolim
Posts: 38
|
Posted 10:47 Feb 19, 2019 |
After I rewrote #3 with pattern matching, I got ghci> fib_top_down_iter 32 My previous version took over 45 seconds. Without any optimization, only using pattern matching the efficiency has been improved 8 - 9 times! In addition, the length of code has been reduced by 1/3!
|
rabbott
Posts: 1649
|
Posted 13:41 Feb 19, 2019 |
Good! That's about what I got running the code above (with no trace).
I wonder why yours took so much more memory: |
jungsoolim
Posts: 38
|
Posted 13:54 Feb 19, 2019 |
It may due to not using Haskell features correctly. Here is what I am doing. data FibElement = I Integer | Fib Integer | Plus | Cache Integer deriving Show fib_top_down_iter n = fib_top_down_iter_helper ([Fib n], []) What portion of the program uses excessive memory? How could I improve it? Any comments? Thanks! |
rabbott
Posts: 1649
|
Posted 18:11 Feb 19, 2019 |
There doesn't seem to be anything terribly wasteful. I wonder if it's due to a combination of (a) using (I n) on the stack instead of just n as my code does and (b) putting the result of an operation back on the inp rather than directly on the stack. |