- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在博客文章中,http://galvanist.com/post/83741037068/adding-badly-under-python-julia-go ,作者使用一个简单的算法来比较各种语言(包括Haskell)的性能。在Haskell的例子中,作者使用了递归函数。作为练习,我想使用 ST monad 来允许本地可变状态。这可行,但递归函数比我使用 ST monad 的函数快得多。
递归函数-
peanoAdd :: Int -> Int -> Int
peanoAdd 0 y = y
peanoAdd x y = peanoAdd (x - 1) (y + 1)
main :: IO ()
main = do
let a = 64000000 :: Int
let b = 64000000 :: Int
let n = peanoAdd a b
print n
128000000
real 0m0.583s
user 0m0.480s
sys 0m0.096s
使用 ST monad-
import Control.Monad.ST
import Data.STRef
import Control.Monad.Loops
peanoAdd :: Int -> Int -> Int
peanoAdd x y = runST $ do
x' <- newSTRef x
y' <- newSTRef y
whileM_ (do x'' <- readSTRef x'
return $ x'' /= 0)
(do modifySTRef x' (subtract 1)
modifySTRef y' (+1))
readSTRef y'
main :: IO ()
main = do
let a = 64000000 :: Int
let b = 64000000 :: Int
let n = peanoAdd a b
print n
128000000
real 0m17.837s
user 0m16.412s
sys 0m1.424s
我做的事情是否明显错误,从而损害了 ST monad 示例中的性能? (PS。我在这两个项目中都使用 Stack 和简单模板。)
最佳答案
ST 程序运行缓慢的一个原因是您正在使用 modifySTRef
, which is non-strict :
Be warned that
modifySTRef
does not apply the function strictly. This means if the program callsmodifySTRef
many times, but seldomly uses the value, thunks will pile up in memory resulting in a space leak. This is a common mistake made when using an STRef as a counter. For example, the following will leak memory and likely produce a stack overflow:print $ runST $ do
ref <- newSTRef 0
replicateM_ 1000000 $ modifySTRef ref (+1)
readSTRef ref
你的x'
每个循环都会被强制一次,但是y'
直到print
才会被强制,所以有一个巨大的链thunk 建立起来。
在我的笔记本电脑上对使用 modifySTRef'
的版本进行基准测试,显示严格性如何改善运行时间(尽管两者仍然输给递归版本)。
benchmarking rec
time 7.896 ms (7.602 ms .. 8.269 ms)
0.992 R² (0.988 R² .. 0.997 R²)
mean 7.842 ms (7.724 ms .. 8.001 ms)
std dev 404.5 μs (303.9 μs .. 523.8 μs)
variance introduced by outliers: 25% (moderately inflated)
benchmarking st
time 18.44 ms (17.84 ms .. 19.01 ms)
0.996 R² (0.993 R² .. 0.998 R²)
mean 18.03 ms (17.79 ms .. 18.41 ms)
std dev 750.4 μs (528.0 μs .. 1.110 ms)
variance introduced by outliers: 16% (moderately inflated)
benchmarking st'
time 9.191 ms (9.028 ms .. 9.437 ms)
0.996 R² (0.992 R² .. 0.999 R²)
mean 9.317 ms (9.175 ms .. 9.527 ms)
std dev 475.8 μs (311.8 μs .. 677.9 μs)
variance introduced by outliers: 25% (moderately inflated)
基准测试代码:
import Criterion.Main
import Control.Monad.ST
import Data.STRef
import Control.Monad.Loops
peanoAddST :: Int -> Int -> Int
peanoAddST x y = runST $ do
x' <- newSTRef x
y' <- newSTRef y
whileM_ (do x'' <- readSTRef x'
return $ x'' /= 0)
(do modifySTRef x' (subtract 1)
modifySTRef y' (+1))
readSTRef y'
peanoAddST' :: Int -> Int -> Int
peanoAddST' x y = runST $ do
x' <- newSTRef x
y' <- newSTRef y
whileM_ (do x'' <- readSTRef x'
return $ x'' /= 0)
(do modifySTRef' x' (subtract 1)
modifySTRef' y' (+1))
readSTRef y'
peanoAddRec :: Int -> Int -> Int
peanoAddRec 0 y = y
peanoAddRec x y = peanoAddRec (x - 1) (y + 1)
main =
let n = 64000 in
defaultMain
[ bench "rec" $ whnf (peanoAddRec n) n
, bench "st" $ whnf (peanoAddST n) n
, bench "st'" $ whnf (peanoAddST' n) n
]
关于haskell - 使用 ST monad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713662/
monad 被定义为类别 C 上的内仿函数。假设 C 具有类型 int 和 bool 以及其他构造类型作为对象。现在让我们考虑在这个类别上定义的列表 monad。 根据它的定义,list 是一个内仿函
我试图采取例如ExceptT a (StateT A M) , 对于某些具体类型 A和单子(monad)M ,并将它们包装到我的新自定义单子(monad)中。 首先我确定StateT A M经常出现在
我读到(例如 here 和 here )所有基本单子(monad)(Mabye, Error, ...) 源自其相应的 monad 转换器(MaybeT, ErrorT, ...) 使用身份 mona
Haskell 的状态单子(monad) State s a迫使我保持相同类型的 s在整个做 block 期间。但是由于 state monad 实际上只是一个函数,如果我将它定义为 State
我一直在阅读some materials on free monads而且我真的不认为我离实现更近了,但我认为我更接近于理解它们是什么! 鉴于上述大量资源,我的理解是自由单子(monad)从“计算”工
假设我有一个由两个 monad 操作组成的函数: co::Monad m => m a -> m a -> m a 您可以将 co 视为一个高阶函数,它描述两个单子(monad)操作如何相互协作来完成
在 SO解释了为什么像 scalaz、cats (Scala) 或 Arrow (Kotlin) 中的 Validation 不能是 monad。 据我所知,这是因为他们已经根据应用仿函数对 mona
我对 Haskell 还很陌生,并且慢慢地意识到 Monad fail 的存在有问题。真实世界的 Haskell warns against its use (“再一次,我们建议您几乎总是避免使用失败
我正在阅读现实世界 Haskell 中的 monad 转换器。在以下示例中,堆栈为 Writer在顶部State在Reader之上在IO之上。 {-# Language GeneralizedNewt
我看到的典型 Pause monad 实现如下所示(基于 Giulia Costantini 和 Giuseppe Maggiore 编写的 Friendly F# 的第 5 章)。 open Sys
“Monads 允许程序员使用顺序构建 block 来构建计算”,因此它允许我们组合一些计算。如果是这样,那为什么下面的代码不能运行呢? import Control.Monad.Trans.Stat
这是我第一次认识 Monad Transformers,所以答案可能很明显。 假设我在 StateT MyMonad MyType 类型的 do 块中,我想让另一个相同类型的函数修改状态并返回 MyM
人们通常说类型是单子(monad)。 在某些函数式语言和库(如 Scala/Scalaz)中,您有一个类型构造函数,如 List 或 Option,您可以定义一个与原始类型分离的 Monad 实现。所
我的目标是创建一个函数,该函数在 ReaderT WriterT 堆栈或 RWS 堆栈中使用 list monad。更一般地说,我如何在 mtl 类型类(如 MonadReader、MonadWrit
我只是想知道是否有一个简洁的术语来表示既是单子(monad)又是单子(monad)的东西。我做了一些搜索,我知道these structures exist ,但我还没有找到他们的名字。 最佳答案 在
我正在玩写一个网络应用程序。在这种情况下,我使用 scotty和 redis ,但是这个问题出现在任何 web/db 组合中。在此之前我使用了 happstack,所以我也喜欢那里的一个例子。 Sco
是 x >>= f相当于 retract (liftF x >>= liftF . f) ? 也就是说,从同样是 Monad 的 Functor 构建的自由 monad 的 monad 实例是否将具有
我正在尝试编写一个只能包含 Num 的新 monad。当它失败时,它返回 0,就像 Maybe monad 在失败时返回 Nothing 一样。 这是我到目前为止所拥有的: data (Num a)
我正在使用 operational monad作者:海因里希·阿普菲尔姆斯。 我想用结果类型的 monad 参数化解释器。 我的代码的以下版本编译: {-# LANGUAGE GADTs #-} im
假设所有的 monad 都可以用 Free 来表示。 (如果这不是真的,什么是反例,为什么)?怎么可能the continuation monad或其对应的变压器用 Free 表示或 FreeT -
我是一名优秀的程序员,十分优秀!