I need to sort rows of large integer matrices in Haskell, and I started doing a comparative analysis with random data. I found that Haskell is 3 times slower than C ++.
Due to randomness, I expect that row comparisons always end in the first column (which should not have duplicates). Therefore, I narrowed the matrix to one column, implemented as a vector (Unboxed.Vector Int), and compared its sorting with the usual Vector Int.
Vector Int is sorted as fast as C ++ (good news!), But again, the column matrix is 3 times slower. Do you have an idea why? Please find the code below.
import qualified Data.Vector.Unboxed as UV(Vector, fromList) import qualified Data.Vector as V(Vector, fromList, modify) import Criterion.Main(env, bench, nf, defaultMain) import System.Random(randomIO) import qualified Data.Vector.Algorithms.Intro as Alg(sort) randomVector :: Int -> IO (V.Vector Int) randomVector count = V.fromList <$> mapM (\_ -> randomIO) [1..count] randomVVector :: Int -> IO (V.Vector (UV.Vector Int)) randomVVector count = V.fromList <$> mapM (\_ -> do x <- randomIO return $ UV.fromList [x]) [1..count] benchSort :: IO () benchSort = do let bVVect = env (randomVVector 300000) $ bench "sortVVector" . nf (V.modify Alg.sort) bVect = env (randomVector 300000) $ bench "sortVector" . nf (V.modify Alg.sort) defaultMain [bVect, bVVect] main = benchSort
source share