[Haskell-cafe] Re: Profiling nested case
dons at galois.com
Fri Jul 18 19:14:34 EDT 2008
> Mitar wrote:
> > On Fri, Jul 18, 2008 at 3:54 PM, Chaddaï Fouché
> > <chaddai.fouche at gmail.com> wrote:
> >>> So that I can easily change the type everywhere. But it would be much
> >>> nicer to write:
> >>> data Quaternion a = Q !a !a !a !a deriving (Eq,Show)
> >>> Only the performance of Num instance functions of Quaternion is then
> >>> quite worse.
> >> You can probably use a specialization pragma to get around that.
> > But why is this not automatic? If I use Quaternions of only one type
> > in the whole program then why it does not make specialized version for
> > it? At least with -O2 switch.
> You could try jhc: it does whole program optimization. Ghc compiles each
> module separately.
No need to switch compilers. GHC is able to do a pretty good job.
data Q a = Q !a !a !a !a deriving (Eq,Show)
-- yeah, polymorphic
go :: Num a => Q a -> Q a
go (Q 0 0 0 0) = Q 1 2 3 4
go (Q a b c d) = go $! Q (a * a) (b * b) (c * c) (d * d)
-- ah, but we fix it.
type QI = Q Int
-- and try that:
main = print (go (Q 2 3 7 13 :: QI))
GHC specialises and gives us,
$wgo :: Int# -> Int# -> Int# -> Int# -> Q Int
So just use ghc-core to check what you're getting.
More information about the Haskell-Cafe