我具有以下类型和两个相关功能,作为大型清单的一部分,我打算对其进行测量:
类型和访问功能:
data Aggregate a = Aggregate (Maybe a) (a -> Aggregate a)
get :: Aggregate a -> Maybe a
get (Aggregate get' _) = get'
put :: Aggregate a -> a -> Aggregate a
put (Aggregate _ put') = put'
第一个功能:
updateFirst :: Maybe a -> a -> Aggregate a
updateFirst cur val = Aggregate new (updateFirst new)
where new = mplus cur (Just val)
first :: Aggregate a
first = Aggregate Nothing (updateFirst Nothing)
第二功能:
updateMinimum :: Ord a => Maybe a -> a -> Aggregate a
updateMinimum cur val = Aggregate new (updateMinimum new)
where new = min <$> (mplus cur (Just val)) <*> Just val
minimum :: Ord a => Aggregate a
minimum = Aggregate Nothing (updateMinimum Nothing)
函数的编写方式应使内存保持恒定。因此,我选择在GHC中使用Strict
语言扩展名,该扩展名要求评估这些代码。 Weigh
库用于执行分配测量:
test :: A.Aggregate Double -> Int -> Maybe Double
test agg len = A.get $ F.foldl' A.put agg (take len $ iterate (+0.3) 2.0)
testGroup :: String -> A.Aggregate Double -> Weigh ()
testGroup name agg = sequence_ $ map (\cnt -> func (str cnt) (test agg) cnt) counts
where
counts = map (10^) [0 .. 6]
str cnt = name ++ (show cnt)
main :: IO ()
main =
mainWith
(do setColumns [Case, Allocated, Max, GCs]
testGroup "fst" A.first
testGroup "min" A.minimum
)
Weigh
输出如下:
Case Allocated Max GCs
fst1 304 96 0
fst10 2,248 96 0
fst100 21,688 96 0
fst1000 216,088 96 0
fst10000 2,160,088 96 2
fst100000 21,600,088 96 20
fst1000000 216,000,088 96 207
min1 552 96 0
min10 4,728 96 0
min100 46,488 96 0
min1000 464,088 96 0
min10000 4,967,768 96 4
min100000 49,709,656 6,537,712 44
min1000000 497,226,840 103,345,512 445
为什么GHC突然在大小为10 ^ 5和10 ^ 6的输入中分配更多的内存?我的GHC版本是8.4.4
。
Haskell中的严格注释可以说是“关系”的。他们说,每当对WHNF进行评估时,都必须对WHNF(weak head normal form)进行评估。