I am trying to figure out how to optimize the code. There he is:
{-
data Vec3 a = Vec3 !a !a !a
vx :: Vec3 a -> a
vx (Vec3 x _ _) = x
{-
vy :: Vec3 a -> a
vy (Vec3 _ y _) = y
{-
vz :: Vec3 a -> a
vz (Vec3 _ _ z) = z
{-
dot :: (Num a) => Vec3 a -> Vec3 a -> a
dot u v = (vx u * vx v) + (vy u * vy v) + (vz u * vz v)
{-
type Vec3D = Vec3 Double
-- just make a bunch of vecs to measure performance
n = 1000000 :: Double
v1s = [Vec3 x y z | (x, y, z) <- zip3 [1 .. n] [2 .. n + 1] [3 .. n + 2]]
:: [Vec3D]
v2s = [Vec3 x y z | (x, y, z) <- zip3 [3 .. n + 2] [2 .. n + 1] [1 .. n]]
:: [Vec3D]
dots = zipWith dot v1s v2s :: [Double]
theMax = maximum dots :: Double
main :: IO ()
main = putStrLn $ "theMax: " ++ show theMax
When I compile with ghc 6.12.1 (ubuntu linux on i486 machine)
ghc --make -O2 Vec.hs -prof -auto-all -fforce-recomp
and run
Vec + RTS -p
Looking at the Vec.prof file,
COST CENTRE MODULE %time %alloc
v2s Main 30.9 36.5
v1s Main 27.9 31.3
dots Main 27.2 27.0
CAF GHC.Float 4.4 5.2
vy Main 3.7 0.0
vx Main 2.9 0.0
theMax Main 2.2 0.0
I see that the vx and vy function takes up a significant part of the time.
Why? I thought the pragma SPECIALIZE INLINE these functions disappear.
When not polymorphic
data Vec3D = Vec3D {vx, vy, vz :: !Double} deriving Show
vx, vy, vz functions are not displayed as cost centers.
source
share