Last active
December 23, 2015 11:59
-
-
Save virtualsafety/6632687 to your computer and use it in GitHub Desktop.
Fibonacci数列两种实现方式效率比较
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keliven@keliven:~/haskell_example$ cat fib_3.hs | |
import System.Environment | |
fib = 0:1:(zipWith (+) fib (tail fib)) | |
main = do | |
args <- getArgs | |
print $ (fib !!) $ read $ args !! 0 | |
keliven@keliven:~/haskell_example$ cat fib_4.hs | |
import System.Environment | |
fib :: Int -> Int | |
fib 0 = 0 | |
fib 1 = 1 | |
fib n = (fib (n-1)) + (fib (n-2)) | |
main = do | |
args <- getArgs | |
print $ fib $ read $ args !! 0 | |
keliven@keliven:~/haskell_example$ time ./fib_4 50 | |
12586269025 | |
real 2m55.257s | |
user 2m54.988s | |
sys 0m0.000s | |
keliven@keliven:~/haskell_example$ time ./fib_3 50 | |
12586269025 | |
real 0m0.003s | |
user 0m0.000s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment