-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMain.hs
50 lines (39 loc) · 1.45 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Data.Time.Clock
import Control.DeepSeq
import Data.Monoid
import Control.Monad
------------------------------
timeIt :: (NFData a) => a -> IO ()
timeIt e = do
start <- getCurrentTime
end <- e `deepseq` getCurrentTime
print $ diffUTCTime end start
------------------------------
main = do
let testData = replicate 2000 [1..100]
let funcs = [("concat", concat),
("leftConcat", leftConcat),
("rightConcat", rightConcat),
("leftConcatWithDlist", leftConcatWithDlist),
("rightConcatWithDlist", rightConcatWithDlist)]
forM_ funcs (\(fname, f) -> do
putStr $fname ++ " : "
timeIt $ length $f testData
)
------------------------------
newtype Dlist a = Dlist (a->a)
toDlist :: (Monoid a) => a -> Dlist a
toDlist l = Dlist (l `mappend`)
fromDlist :: (Monoid a) => Dlist a -> a
fromDlist (Dlist f) = f mempty
dlistConcat :: Dlist a -> Dlist a -> Dlist a
dlistConcat (Dlist f1) (Dlist f2) = Dlist (f1 . f2)
------------------------------
leftConcat :: (Monoid a) => [a] -> a
leftConcat l = foldl mappend mempty l
rightConcat :: (Monoid a) => [a] -> a
rightConcat l = foldr mappend mempty l
leftConcatWithDlist :: (Monoid a) => [a] -> a
leftConcatWithDlist l = fromDlist $ foldl dlistConcat (toDlist mempty) $ map toDlist l
rightConcatWithDlist :: (Monoid a) => [a] -> a
rightConcatWithDlist l = fromDlist $ foldr dlistConcat (toDlist mempty) $ map toDlist l