[Haskell-cafe] Small question

John Meacham john at repetae.net
Thu Aug 9 21:15:56 EDT 2007


On Thu, Aug 09, 2007 at 06:37:32PM +0100, Andrew Coppin wrote:
> Which of these is likely to go faster?
>  type Quad = (Bool,Bool)
...
>  data Quad = BL | BR | TL | TR
...
> I'm hoping that the latter one will more more strict / use less space. 
> But I don't truely know...

The second one will be signifigantly better for a couple reasons. A
simple counting of values that they can take on will show not only this
but that they are not isomorphic even, 

(Bool,Bool) can be one of

_|_ 
(True,True) 
(True,False) 
(False,True) 
(False,False) 
(_|_,True)
(_|_,False) 
(_|_,_|_) 
(True,_|_) 
(False,_|_)

that is a total of 10 different cases, each time a bottom might appear,
a thunk evaluation (or indirection) is involved.


now, take the second case

data Quad = BL | BR | TL | TR

the possible values are

_|_
BL
BR
TL
TR

a whole half of the other representation.


under jhc (and probably ghc at some point in the future) there is another
very strong advantage to the second one, since it is an enumerated type,
internally it can be represented by a simple unboxed byte that takes on
a value of 0,1,2,or 3, which is a very enabling optimization, especially
in the 'if' case in your code.

        John



-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list