Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Why Integral makes a function much slower than Int? (Jaakko Luttinen) ---------------------------------------------------------------------- Message: 1 Date: Sat, 8 Sep 2018 17:19:13 +0300 From: Jaakko Luttinen <jaakko.lutti...@iki.fi> To: beginners@haskell.org Subject: [Haskell-beginners] Why Integral makes a function much slower than Int? Message-ID: <b7e20aac-b082-9c4d-0c78-872bfafec...@iki.fi> Content-Type: text/plain; charset=utf-8; format=flowed Hi all, I implemented a simple function to calculate the length of a list. However, I noticed that the speed of the function changes significantly if I make I minor change to the type signature but keep the implementation the same otherwise. I was wondering why this happens. Contents of mylength.hs: module MyLength where -- Here is the first implementation: myLength :: Integral n => [a] -> n myLength xs = run xs 0 where run [] n = n run (_:ys) n = run ys (n+1) -- Then, the other implementation just fixes the return type without -- modifying the actual implementation: myLength' :: [a] -> Int myLength' = myLength When I compile these implementations with: ghc -dynamic -O2 mylength.hs Then, launch ghci: ghci -fobject-code And in ghci: :load mylength.hs :set +s (myLength [1..10000000]) :: Int 10000000 (2.30 secs, 1,612,737,944 bytes) (myLength' [1..10000000]) :: Int 10000000 (0.38 secs, 720,077,536 bytes) So, the first implementation is much worse, although I've fixed the return type to Int too. What's going on? Does this mean one shouldn't use Integral but Int instead most of the time? Thanks for help! Regards, Jaakko ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 123, Issue 4 *****************************************