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

In reply to GHC shuffle more efficient than Perl5. by audreyt
in thread Why is the execution order of subexpressions undefined? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.