Haskell中构造函数的内存使用情况 [英] Memory usage of constructors in haskell

查看:110
本文介绍了Haskell中构造函数的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

Haskell数据类型的内存占用情况


组合问题,我经常将解决方案表示为一个字符串,例如。 1010100010110111000110 ...你得到的图片。



我认为当我使用 [Int] 作为位串, Int 总是花费相同数量的内存,不管实际数量有多大(因为 Int 它是有界的,与 Integer 不同),因为计算机只记住位表示,并且 String 会占用更多的空间据我所知。



然后我的想法是使用数据类型

 数据位数=空|零位| One Bits派生(Eq,Ord,Show)

但是构造函数的内存多少空,一个使用与比较's?

解决方案

Int 内存中有两个字( #I 构造函数和 #Int 字段),您的位 code>数据可以使用各种成本,例如:零(一(零空))将花费:

构造函数

  • / code>构造函数和字段
  • 一个构造函数和字段

  • > 零字构造函数和字段

  • 和总成本 - 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:

    1. One word for Empty constructor
    2. Two words for Zero Constructor and field
    3. Two words for One Constructor and field
    4. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆