【发布时间】:2015-11-15 18:23:24
【问题描述】:
我正在玩 Haskell 并编写文件。 这是一小段代码,它为 [0..255] 中的每个“蓝色”创建一个 PPM 中的图像,其中“红色”和“绿色”在 [0..255] 中。它在一个文件夹中生成所有图像。
这个小程序使用超过 5 GB 的内存使我的电脑崩溃,我不知道为什么。我让他做的事情对我来说似乎很简单,生成的图像占用的总空间是 - 200 mB ...而且不明白为什么内存会爆炸。
代码如下:
import System.Directory
import Data.Colour.SRGB
import Data.List.Split
gridColours = [[RGB 255 0 0, RGB 0 255 0], [RGB 0 0 255, RGB 0 0 0]]
gridColours2 b = chunksOf 256 [RGB r g b| r <- [0..255], g <- [0..255]]
dimensions l = (length . head $ l, length l)
coupleToList (a,b) = [a,b]
genDataLines list = unwords . (map showColour) . concat $ list
genDims list = unwords . (map show) . coupleToList . dimensions $ list
writePPM list path = writeFile path $ unwords $ ["P3", genDims list, "255", genDataLines list]
showColour c = unwords . (map show) $ [channelRed c, channelGreen c, channelBlue c]
main = do
createDirectoryIfMissing True "/tmp/PPM-out/"
mapM_ (\x -> writePPM (gridColours2 x) ("/tmp/PPM-out/image"++(show x)++".ppm")) [0..255]
一个完美的答案将解释我做错了什么,并就如何解决我的系统崩溃的内存问题给我建议。
谢谢。
【问题讨论】: