Int vs Word performance?

Don Stewart dons at galois.com
Fri Feb 27 19:40:55 EST 2009


marlowsd:
> Claus Reinke wrote:
>>>> Here is a trivial example with drastic difference between
>>>> T = Int and T = Word (~2.5x here):
>>>>
>>>>    main = print $ foldl' (+) 0 [1..100000000::T]
>> ..
>
>> A quick grep shows almost no specialization at all for Word, or for
>> IntXX/WordXX (see below). Still, none of that seems to explain the
>> example repeated at the top of this message.
>
> The Enum instance for Int uses specialised implementations of enumFromTo  
> and friends, whereas the Word version uses the generic 
> integralEnumFromTo.

Another good reason to use uvector,

    import Data.Array.Vector
    import Data.Word

    main = print $ foldlU (+) (0::T) (enumFromToU 1 (100000000::T))

type T = Word


    $wfold :: Word# -> Word# -> Word#

    $wfold =
      \ (ww_s1cg :: Word#) (ww1_s1ck :: Word#) ->
        case gtWord# ww1_s1ck __word 100000000 of wild_a19p {
          False ->
            $wfold
              (plusWord# ww_s1cg ww1_s1ck)
              (plusWord# ww1_s1ck __word 1);
          True -> ww_s1cg

Yields:

    Main_zdwfold_info:
    .Lc1e1:
      cmpq $100000000,%rdi
      ja .Lc1e4
      leaq 1(%rdi),%rax
      addq %rdi,%rsi
      movq %rax,%rdi
      jmp Main_zdwfold_info

While at 

    type T = Int

We get:

    $wfold :: Int# -> Int# -> Int#

    $wfold =
      \ (ww_s144 :: Int#) (ww1_s148 :: Int#) ->
        case ># ww1_s148 100000000 of wild_a11q {
          False ->
            $wfold
              (+# ww_s144 ww1_s148) (+# ww1_s148 1);
          True -> ww_s144

And *identical assembly*

    Main_zdwfold_info:
    .Lc15E:
      cmpq $100000000,%rdi
      jg .Lc15H
      leaq 1(%rdi),%rax
      addq %rdi,%rsi
      movq %rax,%rdi
      jmp Main_zdwfold_info

-- Don


More information about the Glasgow-haskell-users mailing list