[Haskell-cafe] Re: sha1 implementation thats "only" 12 times slower then C

Dominic Steinitz dominic.steinitz at blueyonder.co.uk
Sat Jul 14 03:53:48 EDT 2007

Anatoly Yakovenko <aeyakovenko <at> gmail.com> writes:

> So I tried implementing a more efficient sha1 in haskell, and i got to
> about 12 times slower as C.  The darcs implementation is also around
> 10 to 12 times slower, and the crypto one is about 450 times slower.
> I haven't yet unrolled the loop like the darcs implementation does, so
> I can still get some improvement from that, but I want that to be the
> last thing i do.

I've been meaning to reply to this for some time but work and domestic duties 
have intervened.

1. Very good.

2. It has type hash::BS.ByteString -> IO [Word] but hash is a pure function. 
Can this be changed?

3. I haven't tried but I assume it only runs with ghc and not hugs? I guess if 
point 1 could be addressed then we could put it in the crypto library (assuming 
you are happy with this) with some sort of conditional flag to use your code if 
the library is being built for ghc but to use the slow version for other 
compilers / interpreters.

On a more discursive note, I still don't think we have found the holy grail 
here: idiomatic functional programming (probably not using StorableArray and 
unsafeRead and unsafeWrite) and lightning fast speed.


PS I noticed you have:

splitByN::Int -> BS.ByteString -> [BS.ByteString]
splitByN nn ll = st : (if (BS.null en) then [] else (splitByN nn en))
      (st,en) = BS.splitAt nn ll

It's a function I often use:

splitByN n =
   unfoldr k
         k [] = Nothing
         k p = Just (splitAt n p)

Maybe it should be a standard part of List and ByteString? 

More information about the Haskell-Cafe mailing list