7

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?

Community
  • 1
  • 1
Undreren
  • 2,811
  • 1
  • 22
  • 34
  • 2
    An `Int` is always either 32 or 64 bits, so it can't store arbitrarily large numbers. `Integer`, on the otherhand, is unbounded. – huon Sep 03 '12 at 09:47
  • 5
    Irrelevant to your question, but there is Data.Bits which has bitfield stuff – Squidly Sep 03 '12 at 09:51
  • @dbaupp : I know that, that is why I wanted it compared to only `Int`'s – Undreren Sep 03 '12 at 09:51
  • 2
    I once spent a long time carefully coding my information in binary and doing low-level bitmask operations on `Int`s because I felt I would have a big performance gain on using an abstract data type that represented my problem clearly. I was wrong. ghc compiled them both to be fast! Are you sure `0` and `1` are the clearest way for your problem? Can you reduce memory usage more effectively by carefully choosing strict and lazy? [However, this is an interesting question for general understanding.] – AndrewC Sep 03 '12 at 10:05
  • A representation of `[Bool]` is more space-efficient. – is7s Sep 03 '12 at 12:24

1 Answers1

10

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.

Fedor Gogolev
  • 10,391
  • 4
  • 30
  • 36