Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

GHC shuffle more efficient than Perl5.

by audreyt (Hermit)
on Apr 22, 2005 at 14:23 UTC ( #450426=note: print w/ replies, xml ) Need Help??


in reply to Re^24: Why is the execution order of subexpressions undefined?
in thread Why is the execution order of subexpressions undefined?

So, I ported your Perl 5 program to Haskell, and benchmarked both against the 1millionlines.dat generated with this:

for (1..1_000_000) { print int(rand(10)), $/ }
Result on my FreeBSD 5.3 laptop is:
Perl5: 4.619u 1.010s 0:05.89 95.4% 10+79736k 0+0io 0pf+0w
GHC: 3.007u 0.038s 0:03.10 97.7% 323+359k 0+0io 0pf+0w
Note the constant memory use by GHC. The Haskell source code is attached as below.
{-# OPTIONS -O2 #-} import Foreign import Data.Array.IArray import Data.Array.ST import Control.Monad import Control.Monad.ST.Strict import System.IO import System.Random import System.Environment main :: IO () main = do args <- getArgs when (null args) $ error "filename required" fh <- openBinaryFile (head args) ReadMode sz <- return . fromInteger =<< hFileSize fh allocaBytes sz $ \buf -> do hGetBuf fh buf sz ixs <- foldM (build buf) [-1] [0 .. sz-1] gen <- newStdGen let len = length ixs + 1 stu :: ST s (STUArray s Int Int) stu = do arr <- newListArray (1, len) (sz-1:ixs) foldM_ (swap arr) gen [len `div` 2,(len `div` 2)-1..1] return arr display buf . elems $ runSTUArray stu build :: Ptr Word8 -> [Int] -> Int -> IO [Int] build buf ixs ix = do chr <- peek (plusPtr buf ix) :: IO Word8 return $ if chr == 0o12 then (ix:ix:ixs) else ixs swap arr g ix = do let (iy, g') = randomR (1, ix) g x1 <- readArray arr $ ix*2-1; x2 <- readArray arr $ ix*2 y1 <- readArray arr $ iy*2-1; y2 <- readArray arr $ iy*2 writeArray arr (ix*2-1) y1; writeArray arr (ix*2) y2 writeArray arr (iy*2-1) x1; writeArray arr (iy*2) x2 return g' display buf [] = return () display buf (x1:x2:xs) = do hPutBuf stdout (plusPtr buf x2) (x1 - x2) display buf xs


Comment on GHC shuffle more efficient than Perl5.
Select or Download Code
Re: GHC shuffle more efficient than Perl5.
by Anonymous Monk on Apr 22, 2005 at 19:17 UTC
Re: GHC shuffle more efficient than Perl5.
by BrowserUk (Pope) on Apr 22, 2005 at 21:41 UTC

    Then I shall re-write my example in Inline assembler and call it Perl.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      This remark is uncalled for.

      Monads are a part of haskell. Without them the language wouldn't be useful at all. They are an integral feature, and not anything like inline assembler.

      The only difference is that monadic functions and pure ones are separated by the type system, and that previously when haskell was not a very practical language, monads didn't exist at all.

      Otherwise this is a part of haskell as much as sort is a part of perl.

      -nuffin
      zz zZ Z Z #!perl

        The distinction is that in a pure FP language, Autrijus' emminently clever code would not be possible.

        The inclusion of the monad into the previously pure FP Haskell, is testiment to the practical restrictions that pure FP implies. There are some tasks that FP algorithms are far and away the best alternative. There are some tasks that imperative algorithms (with their inherent utilisation of side effects) are more practical.

        Once you remove the "no side-effects" aspect from FP, you end up with the main distinction between FP and imperative code being the syntax used. For some algorithms, a declarative syntax is the most clear and concise. For some algoriths, a recursive definition is the easiest to code, understand and maintain--whether using declarative or imperative syntax. For some algorithms, good ol'imperative loops and blocks and side-effects is just the ticket.

        My preference--and there is no implication in this that seeks to restrict anyone else to my choices--is for a language that supports all these styles of syntax, without artificial restristions. For me, currently, the best choice available is Perl, which is why I come back to Perl in preference to all the other languages I've tried over the last few years.

        My conclusion from Autrujus' example, is that to address the constraints imposed by my posted problem (memory), the requirement is to utilise side-effects. What Autrujus demonstrated was that this can indeed be done within Haskell.

        So, we reach the point where the given task can be tackled in both languages, so the choice of language comes down to personal preference--mine is Perl.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://450426]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-08-30 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls