On Fri, May 3, 2013 at 4:00 PM, Sasha Pachev <[email protected]> wrote:
> Levi - I still would like to see some code in Haskell, even if it is
> dog slow and I have to do some work to get it to run on my box. I
> programmed in Scheme for a class back in 1994 (CS 330), I do remember
> writing a program in C to help me track down my mismatched braces, and
> writing a poem "Count your braces, count them one by one", but that is
> my extent of exposure to functional programming.
Here are a couple of somewhat more efficient versions. This first one
is equivalent to the previous, but uses a more efficient string
storage format:
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import System.IO.Posix.MMap
import qualified Data.ByteString.Char8 as S
import qualified Data.Text as T
import qualified Data.Text.Encoding as E
main = do
bs <- unsafeMMapFile "/usr/share/dict/words"
mapM_ (S.putStrLn . E.encodeUtf8 . T.reverse . E.decodeUtf8) (S.split '\n' bs)
This one ignores unicode like yours does and gains a tiny bit of performance:
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import System.IO.Posix.MMap
import qualified Data.ByteString.Char8 as S
main = do
bs <- unsafeMMapFile "/usr/share/dict/words"
mapM_ (S.putStrLn . S.reverse) (S.split '\n' bs)
On my system, your original strrev.c runs in about 0.016s
My initial Haskell implementation runs in about 0.104s
The one using faster I/O routines but still unicode-safe takes about 0.068s.
The one that assumes single-byte characters takes about 0.45s.
It could most likely be sped up further until it was about the same
speed as the C one, but there's not much point to expending the effort
here.
--Levi
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/