- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将大量数据写入常量内存中的文件。
import qualified Data.ByteString.Lazy as B
{- Creates and writes num grids of dimensions aa x aa -}
writeGrids :: Int -> Int -> IO ()
writeGrids num aa = do
rng <- newPureMT
let (grids,shuffleds) = createGrids rng aa
createDirectoryIfMissing True "data/grids/"
B.writeFile (gridFileName num aa)
(encode (take num grids))
B.writeFile (shuffledFileName num aa)
(encode (take num shuffleds))
但是,这会消耗与 num
大小成比例的内存。我知道 createGrids 是一个足够懒的函数,因为我已经通过将错误“不够懒”(如 Haskell wiki here 所建议的)附加到列表末尾来测试它它返回并且不会引发任何错误。 take
是一个在 Data.List
中定义的惰性函数。 encode
也是 Data.Binary
中定义的惰性函数。 B.writeFile
在 Data.ByteString.Lazy
中定义。
这是完整的代码,您可以执行它:
import Control.Arrow (first)
import Data.Binary
import GHC.Float (double2Float)
import System.Random (next)
import System.Random.Mersenne.Pure64 (PureMT, newPureMT, randomDouble)
import System.Random.Shuffle (shuffle')
import qualified Data.ByteString.Lazy as B
main :: IO ()
main = writeGrids 1000 64
{- Creates and writes num grids of dimensions aa x aa -}
writeGrids :: Int -> Int -> IO ()
writeGrids num aa = do
rng <- newPureMT
let (grids,shuffleds) = createGrids rng aa
B.writeFile "grids.bin" (encode (take num grids))
B.writeFile "shuffleds.bin" (encode (take num shuffleds))
{- a random number generator, dimension of grids to make
returns a pair of lists, the first is a list of grids of dimensions
aa x aa, the second is a list of the shuffled grids corresponding to the first list -}
createGrids :: PureMT -> Int -> ([[(Float,Float)]],[[(Float,Float)]])
createGrids rng aa = (grids,shuffleds) where
rs = randomFloats rng
grids = map (getGridR aa) (chunksOf (2 * aa * aa) rs)
shuffleds = shuffler (aa * aa) rng grids
{- length of each grid, a random number generator, a list of grids
returns a the list with each grid shuffled -}
shuffler :: Int -> PureMT -> [[(Float,Float)]] -> [[(Float,Float)]]
shuffler n rng (xs:xss) = shuffle' xs n rng : shuffler n (snd (next rng)) xss
shuffler _ _ [] = []
{- divides list into chunks of size n -}
chunksOf :: Int -> [a] -> [[a]]
chunksOf n = go
where go xs = case splitAt n xs of
(ys,zs) | null ys -> []
| otherwise -> ys : go zs
{- dimension of grid, list of random floats [0,1]
returns a list of (x,y) points of length n^2 such that all
points are in the range [0,1] and the points are a randomly
perturbed regular grid -}
getGridR :: Int -> [Float] -> [(Float,Float)]
getGridR n rs = pts where
nn = n * n
(irs,jrs) = splitAt nn rs
n' = fromIntegral n
grid = [ (p,q) | p <- [0..n'-1], q <- [0..n'-1] ]
pts = zipWith (\(p,q) (ir,jr) -> ((p+ir)/n',(q+jr)/n')) grid (zip irs jrs)
{- an infinite list of random floats in range [0,1] -}
randomFloats :: PureMT -> [Float]
randomFloats rng = let (d,rng') = first double2Float (randomDouble rng)
in d : randomFloats rng'
所需的包是: , 字节串 , 二进制 , 随机的 , mersenne-random-pure64 ,随机洗牌
最佳答案
内存使用的两个原因:
首先,Data.Binary.encode
似乎没有在恒定空间中运行。以下程序使用 910 MB 内存:
import Data.Binary
import qualified Data.ByteString.Lazy as B
len = 10000000 :: Int
main = B.writeFile "grids.bin" $ encode [0..len]
如果我们在 len
中留下 0
,我们会得到 97 MB 的内存使用量。
相比之下,以下程序使用 1 MB:
import qualified Data.ByteString.Lazy.Char8 as B
main = B.writeFile "grids.bin" $ B.pack $ show [0..(1000000::Int)]
第二,在您的程序中,shuffleds
包含对 grids
内容的引用,这会阻止 grids
的垃圾回收。因此,当我们打印grids
时,我们也会对其进行评估,然后它必须保存在内存中,直到我们完成打印shuffleds
。您的程序的以下版本仍然消耗大量内存,但如果我们用 B.writeFile
注释掉两行之一,它会使用恒定空间。
import qualified Data.ByteString.Lazy.Char8 as B
writeGrids :: Int -> Int -> IO ()
writeGrids num aa = do
rng <- newPureMT
let (grids,shuffleds) = createGrids rng aa
B.writeFile "grids.bin" (B.pack $ show (take num grids))
B.writeFile "shuffleds.bin" (B.pack $ show (take num shuffleds))
关于haskell - 为什么这不能在恒定内存中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541058/
要求: 我们在数据库中有值 Chennai Baroda Bangalore New Delhi São Paulo, Lisboa San Jose 等... 所以我想将这些字符串转换成一个唯一的短
我需要通过 TCP 加密和发送数据(每条消息从几百字节到几百兆字节)以 block 从 Java 到 C++ 程序,并且需要发送的大小提前发送数据,以便收件人知道何时停止读取当前消息并处理它,然后等待
我不太知道如何问这个问题,因此我将其作为示例: 想象在一个应用程序中您有一个Country对象。此对象有两个属性:Name和'Bordering Countries'集合。稍后可能会添加更多属性,但这
我们正在尝试使用 IoT 中心、流分析和表存储来处理仪表板上的事件驱动图标(开/关/事件)。我无法让实际过程在 6 秒内完成。流分析的水印恒定为 5 秒。该网站声称即时处理。 https://azur
我正在尝试获取一个 JTabbedPane,其中所有选项卡(实际选项卡,而不是组件)具有相同的宽度(最宽标签所需的最小宽度或恒定宽度)。 我试图覆盖 BasicTabbedPaneUI.getTabB
你好安卓开发者, 我正在使用 OpenGLES 1.0 在 Eclipse 中为 Android 开发一个简单的游戏。我正在使用 Samsung Galaxy S2 Android(2.3) 作为开发
我正在尝试使用 ColorMatrix 为整个图像设置一个恒定的色调值。我的目标是使整个图像看起来颜色相同,而不会失去任何区域的亮度。我找到了一种使用 ColorMatrix 来移动图像的色调值的方法
我最近将我的 Postgres 数据库从 Windows 迁移到 CentOS 6.7。在 Windows 上,数据库从不使用太多 CPU,但在 Linux 上,我看到它使用恒定的 ~30% CPU(
你好,我正在用 SDL2 开发一款很棒的游戏,我添加了一个 FPS 调节器以使其在每台机器上都能流畅运行,但后来我创建了一个示例程序来输出 fps(不是调节,只是输出以查看电流的功率机器运行 ) 我在
我在 IntelliJ IDEA 13.0 中开发了一个应用程序,但是当我在我的设备上运行该应用程序时,它会不断等待调试器(如果我在 Debug模式下运行,调试器连接时没问题)但是如果远离计算机,我无
我想在 stat_密度2 图中绘制多个组,其中 alpha 值与每组中的观察计数相关。然而,由 stat_密度2d 形成的水平似乎已标准化为每组中的观察数量。例如, temp <- rbind(mov
我是一名优秀的程序员,十分优秀!