【发布时间】:2020-05-18 17:46:00
【问题描述】:
我在尝试运行诸如此类的中等输入时内存不足:
variation_models 15 25
同时为 ncars 运行更高的数字似乎在速度和内存使用方面产生了巨大差异。
预计会放缓(有更多的东西要比较),但内存使用量的指数增长对我来说没有意义
import Control.Monad
orderedq f [] = True
orderedq f (x:[]) = True
orderedq f (x:y:zs) = f x y && orderedq f (y:zs)
num_orderedq = orderedq (<=)
adds_up_to n xs = n == sum xs
both_conditions f g xs = f xs && g xs
variation_models ncars nlocations =
filter (both_conditions (adds_up_to nlocations) num_orderedq) $ replicateM ncars [1..nlocations-ncars+1]
是什么导致了内存使用量的巨大差异? replicateM?
【问题讨论】:
-
我不认为是
replicateM引起了问题,因为take 10 $ replicateM 15 [1..1000000]工作正常 -
我认为是。试试
length $ replicateM 15 [1..11]。 -
这很粗略,但我用过滤器丢弃了大部分结果。我预计丢弃的结果可能是 gc'd
-
length会丢弃所有结果,但如果您真的想被说服,请尝试使用filter (const False)代替length。