Haskell Module Optimization

I have a problem with optimizing the Haskell module.

There is a main module.

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.DeepSeq
import Formatting
import Formatting.Clock
import System.Clock

import Data.Array

size :: Int
size = 200 :: Int

stdMult     :: (Ix a, Ix b, Ix c, Num d) =>
               Array (a,b) d -> Array (b,c) d -> Array (a,c) d
stdMult x y =  array resultBounds
                 [((i,j), sum [ x!(i,k) * y!(k,j) | k <- range (lj,uj)])
                                   | i <- range (li,ui),
                                     j <- range (lj',uj') ]
    where ((li,lj),(ui,uj))     = bounds x
          ((li',lj'),(ui',uj')) = bounds y
          resultBounds
            | (lj,uj)==(li',ui') = ((li,lj'),(ui,uj'))
            | otherwise = error "error"


main :: IO ()
main = do
  let a = array ((1,1),(size, size)) [((i,j), 2*i-j) |
                                  i <- range (1,size),
                                  j <- range (1,size)]
  let b = array ((1,1),(size, size)) [((i,j), 2*i+3*j) |
                                  i <- range (1,size)`,
                                  j <- range (1,size)]

  start <- getTime ProcessCPUTime
  let
    c = stdMult a b
  end <- c `deepseq` getTime ProcessCPUTime
  fprint (timeSpecs % "\n") start end
  return()

When stdMultin the main module, everything works fine. I am replacing stdMultwith another module. When I do not use ghc optimization, the runtime is the same. When I use the ghc -O3 options, when stdMultduring the execution of the main module it decreases, but when stdMultin the other module the execution time remains almost unchanged! For example, when stdMultin Main I have a time of 3 seconds, and when stdMultnot in Main, I have a time of 30 seconds, for a matrix 500x500.

It is very strange!

+6
source share
1 answer

( clock formatting Hackage.)

10- , stdMult . , : , stdMult , INLINABLE :

{-# INLINABLE stdMult #-}

(.hi), , , Int Ix Num . ( GHC , INLINABLE, .)

INLINABLE SPECIALIZE , . , ( , , ).

{-# SPECIALIZE stdMult :: Array (Int, Int) Int -> Array (Int, Int) Int -> Array (Int, Int) Int #-}
+10

All Articles