[Haskell-beginners] Execution time of Mergesort

Awsaf Rahman awsafrahman1704 at gmail.com
Fri Jul 6 02:01:27 UTC 2018


Hello everyone!

I am trying to find out the execution time of mergesort for a list size of
1 million integers. I have done the same in Erlang as well and the
execution time in Erlang was around 0.93 seconds. I have implemented the
program in almost the same way in Haskell as well but for some reason the
Haskell implementation is taking around 12 seconds which doesn't seem
right.

Here is the implementation in Haskell:

*{-# LANGUAGE OverloadedStrings #-}*
*{-# LANGUAGE BangPatterns #-}*
*import Control.Exception*
*import Formatting*
*import Formatting.Clock*
*import System.Clock*
*import Control.DeepSeq*

*mergesort [] = []*
*mergesort [x] = [x]*
*mergesort xs = let (lhalf, rhalf) = splitAt (length xs `div` 2) xs*
*               in merge' (mergesort lhalf) (mergesort rhalf)*

*merge' lhalf rhalf = merge lhalf rhalf []*

*merge [] [] acc = reverse acc*
*merge [] y acc = reverse acc ++ y*
*merge x [] acc = reverse acc ++ x*

*merge (l:ls) (r:rs) acc*
*        | l < r = merge ls (r:rs) (l:acc)*
*        | otherwise = merge rs (l:ls) (r:acc)*

*toList :: String -> [Integer]*
*toList input = read ("[" ++ input ++ "]")*

*main = do*
*    file <- getLine*
*    contents <- readFile file*
*    let !unsortedlist = (toList contents)*
*    start <- getTime Monotonic*
*    evaluate(force (mergesort unsortedlist))*
*    end <- getTime Monotonic*
*    fprint (timeSpecs % "\n") start end*


What am I doing wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180706/30c71f34/attachment.html>


More information about the Beginners mailing list