Int vs Word performance?

Don Stewart dons at
Thu Feb 26 19:28:25 EST 2009

> Looking at prelude/PrelRules.hs has reminded me of an old
> conundrum: if I switch from Int to Word, should I expect any
> performance differences?
> A while ago, I needed lots of fairly small positive numbers,
> together with a small number of flags for each, so I thought
> I'd switch from Int to Word, and map the flags to bits. But the 
> performance dropped so drastically that I went back
> to Int, slightly complicating the bitmaps. I didn't expect that,
> and I can't see any builtin rules for Int that would have no
> Word counterpart.
> Here is a trivial example with drastic difference between
> T = Int and T = Word (~2.5x here):
>    main = print $ foldl' (+) 0 [1..100000000::T]
> What am I missing here?
> Also, in the real code I ended up seeing things like this in
> the -ddump-simpl output for the bit-fiddling code:
>    GHC.Prim.word2Int#
>        (GHC.Prim.and#
>            (GHC.Prim.int2Word# wild13_XbE)
>            (GHC.Prim.int2Word# y#_a4EZ))
> Is that likely to cost me a lot or are these conversions cheap?

Those guys are no-ops, and in general you should never see a performance
difference. If you do, it is a bug!  There are some known cases where
rules are missing however:

  * Conversions to Int from Double for ceil, floor, round are missing rules.

  * gcd only has a specialised implementation for Int,

Some others I'm aware of are product/sum/maximum/minimum
on lists have specialisations for some atomic types (Int, Integer) but
not all (needs a ticket for this too).

I'm not aware of any remaining "theoretically noop" conversions that
aren't in fact implemented noops now. If you find them, please open a

-- Don

More information about the Glasgow-haskell-users mailing list