haskell 中 bst 的计时实验
timing experiment on bst in haskell
嗨,我正在尝试学习 haskell 并将其性能与其他语言进行比较
当我 运行 下面的代码..
module BST (
Tree,
singletonTree,
insert,
member
) where
import System.IO
import System.IO.Error hiding (try)
import Control.Exception
import Data.Char
import System.CPUTime
--
-- Take the string and convert it to a list of numbers
--
trim = f . f
where f = reverse . dropWhile isSpace
fromDigits = foldl addDigit 0
where addDigit num d = 10*num+d
strToInt str = fromDigits (map digitToInt str)
split_comma "" = []
split_comma input =
let (a,b) = break (\x->x==',') input in
[(trim a)]++(split_comma (drop 1 b))
make_int_list input =map strToInt (split_comma input)
-- end of converting string to integers
data Tree a = EmptyTree | Node a (Tree a)(Tree a) deriving (Show)
singletonTree :: a -> Tree a
singletonTree x = Node x EmptyTree EmptyTree
insert :: Ord a => a -> Tree a -> Tree a
insert x EmptyTree = singletonTree x
insert x (Node root left right)
| x < root = Node root (insert x left) (right)
| x > root = Node root (left) (insert x right)
| x == root = Node root (Node x left EmptyTree) (right)
member :: Ord a => a -> Tree a -> Bool
member x EmptyTree = False
member x (Node n left right)
| x == n = True
| x < n = member x left
| x > n = member x right
---A test function to do the timing
test_func input_list =do
startTime <- getCPUTime
--Note: If you don't use any results haskell won't even run the code
-- if you just mergesrt here (uncomment next line) instead of print
-- return (let tree = foldr insert EmptyTree )
-- then it will always take 0 seconds since it won't actually sort!
let tree = foldr insert EmptyTree input_list
prin(tree)
finishTime <- getCPUTime
return $ fromIntegral (finishTime - startTime) / 1000000000000
main :: IO ()
main = do
inh <- openFile "random_numbers.txt" ReadMode
mainloop inh
hClose inh
--Read in my file and run test_func on input
mainloop :: Handle -> IO ()
mainloop inh =
do input <- try (hGetLine inh)
case input of
Left e ->
if isEOFError e
then return ()
else ioError e
Right inpStr ->
do
let my_list = make_int_list inpStr;
my_time <- test_func my_list
putStrLn ("Execution time in Sections: ")
print(my_time);
return ();
尝试运行此代码时,我得到
序曲> :load "bst.hs"
[1 of 1] 编译 BST(bst.hs,已解释)
bst.hs:83:29:输入“<-”时出现解析错误
失败,加载模块:none.
我已经穷尽了我对 haskell 的了解。我尝试将模块语句移动到包含之前和之后,但都没有帮助。我分别使用了 bst 和计时码,但结合起来会导致错误
random_numbers.txt 是逗号分隔值的列表。
最后一个 do
块的格式不正确。这是一个差异:
@@ -78,9 +78,7 @@
then return ()
else ioError e
Right inpStr ->
- do
- let my_list = make_int_list inpStr;
- my_time <- test_func my_list
- putStrLn ("Execution time in Sections: ")
- print(my_time);
- return ();
+ do let my_list = make_int_list inpStr;
+ my_time <- test_func my_list
+ putStrLn("Execution time in Sections: ")
+ print(my_time)
备注:
- 我没有在源代码中的任何地方使用制表符;我有一种感觉你的来源使用标签。我的建议是避免在 Haskell 来源中使用制表符。
- 您不需要括号来调用函数 -
putStrLn "..."
和 print my_time
都可以
此外,之前的 prin(tree)
应该是 print(tree)
但更常见的写法是 print tree
- 不需要括号。
嗨,我正在尝试学习 haskell 并将其性能与其他语言进行比较 当我 运行 下面的代码..
module BST (
Tree,
singletonTree,
insert,
member
) where
import System.IO
import System.IO.Error hiding (try)
import Control.Exception
import Data.Char
import System.CPUTime
--
-- Take the string and convert it to a list of numbers
--
trim = f . f
where f = reverse . dropWhile isSpace
fromDigits = foldl addDigit 0
where addDigit num d = 10*num+d
strToInt str = fromDigits (map digitToInt str)
split_comma "" = []
split_comma input =
let (a,b) = break (\x->x==',') input in
[(trim a)]++(split_comma (drop 1 b))
make_int_list input =map strToInt (split_comma input)
-- end of converting string to integers
data Tree a = EmptyTree | Node a (Tree a)(Tree a) deriving (Show)
singletonTree :: a -> Tree a
singletonTree x = Node x EmptyTree EmptyTree
insert :: Ord a => a -> Tree a -> Tree a
insert x EmptyTree = singletonTree x
insert x (Node root left right)
| x < root = Node root (insert x left) (right)
| x > root = Node root (left) (insert x right)
| x == root = Node root (Node x left EmptyTree) (right)
member :: Ord a => a -> Tree a -> Bool
member x EmptyTree = False
member x (Node n left right)
| x == n = True
| x < n = member x left
| x > n = member x right
---A test function to do the timing
test_func input_list =do
startTime <- getCPUTime
--Note: If you don't use any results haskell won't even run the code
-- if you just mergesrt here (uncomment next line) instead of print
-- return (let tree = foldr insert EmptyTree )
-- then it will always take 0 seconds since it won't actually sort!
let tree = foldr insert EmptyTree input_list
prin(tree)
finishTime <- getCPUTime
return $ fromIntegral (finishTime - startTime) / 1000000000000
main :: IO ()
main = do
inh <- openFile "random_numbers.txt" ReadMode
mainloop inh
hClose inh
--Read in my file and run test_func on input
mainloop :: Handle -> IO ()
mainloop inh =
do input <- try (hGetLine inh)
case input of
Left e ->
if isEOFError e
then return ()
else ioError e
Right inpStr ->
do
let my_list = make_int_list inpStr;
my_time <- test_func my_list
putStrLn ("Execution time in Sections: ")
print(my_time);
return ();
尝试运行此代码时,我得到
序曲> :load "bst.hs" [1 of 1] 编译 BST(bst.hs,已解释)
bst.hs:83:29:输入“<-”时出现解析错误 失败,加载模块:none.
我已经穷尽了我对 haskell 的了解。我尝试将模块语句移动到包含之前和之后,但都没有帮助。我分别使用了 bst 和计时码,但结合起来会导致错误
random_numbers.txt 是逗号分隔值的列表。
最后一个 do
块的格式不正确。这是一个差异:
@@ -78,9 +78,7 @@
then return ()
else ioError e
Right inpStr ->
- do
- let my_list = make_int_list inpStr;
- my_time <- test_func my_list
- putStrLn ("Execution time in Sections: ")
- print(my_time);
- return ();
+ do let my_list = make_int_list inpStr;
+ my_time <- test_func my_list
+ putStrLn("Execution time in Sections: ")
+ print(my_time)
备注:
- 我没有在源代码中的任何地方使用制表符;我有一种感觉你的来源使用标签。我的建议是避免在 Haskell 来源中使用制表符。
- 您不需要括号来调用函数 -
putStrLn "..."
和print my_time
都可以
此外,之前的 prin(tree)
应该是 print(tree)
但更常见的写法是 print tree
- 不需要括号。