-
Notifications
You must be signed in to change notification settings - Fork 141
Closed
Description
After trying a simple test, I noticed some strange performance results from stylistic changes to the code.
For example,
{-# LANGUAGE FlexibleContexts #-}
import Control.DeepSeq
import Data.Int
import qualified Data.Vector.Unboxed as U
{-# INLINE f #-}
f :: U.Vector Int64 -> U.Vector Int64 -> U.Vector Int64
f = U.zipWith (+) -- version 1
--f x = U.zipWith (+) x -- version 2
--f x = (U.zipWith (+) x) . id -- version 3
--f x y = U.zipWith (+) x y -- version 4
main = do
let iters = 100
dim = 221184
y = U.replicate dim 0 :: U.Vector Int64
let ans = iterate (f y) y !! iters
putStr $ (show $ U.foldl1' (+) ans)
Versions 1 and 2 run in 1.6 seconds, while versions 3 and 4 run in 0.09 seconds (with vector-0.10.9.1 and GHC 7.6.2). According to this answer, this problem is because the first two versions use Generic vector code rather than low-level imperative code.
Is there anything that a user can do to ensure the best vector code is used without more or less guessing at a style that will make the rule fire (I'm thinking pragmas, compiler flags, any other hints, intuition about why style "x" is a bad choice, etc)?
Metadata
Metadata
Assignees
Labels
No labels