Good Evening,

can anybody confirm that this implementation is somewhat faster
than the current benchmark (at expense of memory consumption)?

Cheers, Thorsten


On 30.07.2011 23:08, Ben wrote:
> hello cafe-istas --
>
> for those of you who are into these things, a lot of the shootout programs 
> are suffering from "make errors" and thus do not have benchmarks.
>
> http://shootout.alioth.debian.org/u64q/haskell.php
>
> best, ben
>
> Begin forwarded message:
>
>> From: Don Stewart <[email protected]>
>> Date: July 30, 2011 9:52:12 AM PDT
>> To: Ben <[email protected]>
>> Cc: Don Bruce Stewart <[email protected]>
>> Subject: Re: shootout
>>
>> Best to bring this up on haskell-cafe@
>>
>> On Sun, Jul 31, 2011 at 12:34 AM, Ben <[email protected]> wrote:
>>> FYI, a lot of the haskell programs on the shootout suffer from "make 
>>> errors" and thus do not have benchmarks.
>>>
>>> best, ben
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe

{-# LANGUAGE BangPatterns #-}
module Main where

import System.Environment

import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as C
import qualified Data.ByteString as S
import Data.ByteString.Internal

import Data.Word

-- Look Up Table
data P = P !Word8 !Float
type LUT =  [P]

iubs, homs :: LUT
iubs = cdf [('a',0.27),('c',0.12),('g',0.12),('t',0.27),('B',0.02)
           ,('D',0.02),('H',0.02),('K',0.02),('M',0.02),('N',0.02)
           ,('R',0.02),('S',0.02),('V',0.02),('W',0.02),('Y',0.02)]

homs = cdf [('a',0.3029549426680),('c',0.1979883004921)
           ,('g',0.1975473066391),('t',0.3015094502008)]

-- compile LUT from assoc list
cdf :: [(Char,Float)] -> LUT
cdf ls = reverse $ cdf' [] 0 ls
    where cdf' acc _ [] = acc
          cdf' acc !c ((v,k):ls) = cdf' ((P v' c'):acc) c' ls
              where !c'  = k + c
                    !v' = c2w v

-- extract Char from List by Key
choose :: LUT -> Float -> Word8
choose lut !f = choose' lut
    where choose' ((P v k):ls)| f <= k = v
                              | otherwise = choose' ls

-- PRNG
im, ia, ic :: Int
im  = 139968
ia  = 3877
ic  = 29573

data R = R !Float !Int

imd :: Float
imd = fromIntegral im

rand :: Int -> R
rand seed = R newran newseed
    where
        !newseed = (seed * ia + ic) `rem` im
        !newran  = (fromIntegral newseed) / imd
-- /PRNG

-- Write properly aligned output
fasta !n s | n >= 60   = go ts t n 60
           | otherwise = go ts t n n
    where (t:ts) = L.toChunks s
          go ss s !n !m | n == 0              = return ()
                        | ll <  m             = S.putStr   l >> go (tail ss) (head ss) (n-ll) (m-ll)
                        | ll == m && n' >= 60 = S.putStrLn l >> go ss r n' 60
                        | ll == m && n' <  60 = S.putStrLn l >> go ss r n' n'
              where (l,r) = S.splitAt m s
                    ll = S.length l
                    !n' = n-m

-- build cache from PRNG
data Q = Q !Int !Int

cacheUF ls = L.unfoldr go $ Q 42 139968
          where go (Q _ 0)  = Nothing
                go (Q sd n) = Just (choose ls f, Q s (n-1))
                    where (R f s) = rand sd

alu =  C.pack "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
               \GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
               \CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
               \ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
               \GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
               \AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
               \AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"

fastas n = do putStrLn  ">ONE Homo sapiens alu"
              fasta (n*2) $ L.cycle alu
              putStrLn ">TWO IUB ambiguity codes"
              fasta (n*3) $ L.cycle $ cacheUF iubs
              putStrLn ">THREE Homo sapiens frequency"
              fasta (n*5) $ L.drop d $ L.cycle $ cacheUF homs
    where  d = fromIntegral (n*3) `mod` 139968

main = do n <- getArgs >>= readIO . head
          fastas n
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to