我通过使用ghc -prof -auto-all -O solve_sudoku.hs
编译Haskell程序并使用solve_sudoku.exe params +RTS -p
运行来分析Haskell程序。
我有一个应该被调用的函数,但它不会出现在日志中。但问题不同。以下是代码的一部分:
-- Cell of a puzzle
data Cell a = Cell {values :: [a], column :: Int, row :: Int}
updateCell :: [Cell Int] -> Cell Int -> Cell Int
updateCell list cell@(Cell v x y) = Cell v' x y
where v' = if length v < 2 then filter (not . same) v
else filter correct v
correct c = all (good c) [row, column, part]
same c = c `elem` (concat . concatMap chosen
$ [row, column, part])
good a thing = allright required vars
where vars = [a] : callBy thing
chosen thing = filter ((==1) . length) $ callBy thing
necessary f = map values $ filter (id f) list -- this is where problem is
unique = id
sameBy f = ( == f cell) . f
callBy f = necessary $ sameBy f
required = [1..9]
如果我使用此代码运行测试,我会得到以下日志:http://pastebin.com/cxnwLkSM
但如果我用filter (id f) list
替换filter (unique f) list
,我会得到以下信息:http://pastebin.com/nUNBAEp4
问题是:如果通过另一个函数名称调用id
,为什么执行需要两倍的时间?我想这与在懒惰评估期间记住评估值有关,但我不能真正理解它。
如果同时删除unique
和id
,则执行速度会比使用unique f
稍慢。
我正在运行安装了Haskell平台的Windows 7 x64,Glassgow Haskell编译器。这是ghc -v
的输出:
Glasgow Haskell Compiler, Version 7.6.3, stage 2 booted by GHC version 7.4.1
Using binary package database: C:\Program Files (x86)\Haskell Platform\2013.2.0.0\lib\package.conf.d\package.cache
Using binary package database: C:\Users\idji\AppData\Roaming\ghc\i386-mingw32-7.6.3\package.conf.d\package.cache
hiding package Cabal-1.16.0 to avoid conflict with later version Cabal-1.18.1.2
hiding package binary-0.5.1.1 to avoid conflict with later version binary-0.7.1.0
hiding package containers-0.4.2.1 to avoid conflict with later version containers-0.5.0.0
wired-in package ghc-prim mapped to ghc-prim-0.3.0.0-9d603ae4e062e1401099a3daa8931d0f
wired-in package integer-gmp mapped to integer-gmp-0.5.0.0-4093e6ff1e8ebdba04ec892df9ac108f
wired-in package base mapped to base-4.6.0.1-f0c2cc6dcf0e12bf75312a2e7f354095
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.8.0.0-42f1e6e55511ef4dff7e4249580faf8f
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: -static