Haskell中构造函数的内存使用情况 [英] Memory usage of constructors in haskell
问题描述
可能存在重复:
Haskell数据类型的内存占用情况
组合问题,我经常将解决方案表示为一个字符串,例如。 1010100010110111000110 ...你得到的图片。
我认为当我使用 [Int]
作为位串, Int
总是花费相同数量的内存,不管实际数量有多大(因为 Int
它是有界的,与 Integer
不同),因为计算机只记住位表示,并且 String
会占用更多的空间据我所知。
然后我的想法是使用数据类型
数据位数=空|零位| One Bits派生(Eq,Ord,Show)
但是构造函数的内存多少空,
零
和一个
使用与比较's?
#I
构造函数和 #Int
字段),您的位
零(一(零空))
将花费:
空
构造函数
一个
构造函数和字段零字
构造函数和字段和总成本 - 7个字。
因此,您的数据的内存量可能超过 Int
。
Possible Duplicate:
Memory footprint of Haskell data types
When solving combinatorial problems, I will often represent the solution as a bit string, eg. 1010100010110111000110... You get the picture.
I figured that when I use [Int]
for the bit string, Int
always spends the same amount of memory, no matter how big the number actually is (because Int
it's bounded, in contrast to Integer
), as the computer only remembers the bit representation, and String
's would take even more space as far as I know.
My idea was then to use the data type
data Bits = Empty | Zero Bits | One Bits deriving (Eq,Ord,Show)
But how much memory do the constructors Empty
, Zero
and One
use compared to Int
's?
Int
costs two words in memory (#I
constructor and #Int
field), your Bits
data can use various cost, for example: Zero (One (Zero Empty))
will cost:
- One word for
Empty
constructor - Two words for
Zero
Constructor and field - Two words for
One
Constructor and field - Two words for
Zero
Constructor and field
and total cost — 7 words.
So memory amount for your data can be more than for Int
.
这篇关于Haskell中构造函数的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!