[Shootout-list] Updates for Haskell benchmarks.
JP Bernardy
jyp_7@yahoo.com
Sun, 27 Jun 2004 08:16:43 -0700 (PDT)
--0-733717733-1088349403=:46778
Content-Type: text/plain; charset=us-ascii
Content-Id:
Content-Disposition: inline
--- "Brent A. Fulgham" <bfulgham@debian.org> wrote:
> Thanks for taking the time to write these
> contributions.
> Unfortunately, I had a few problems
Obviously I overlooked testing... Sorry.
> >hash
> >
> >
> This does not produce a result for me. Can you
> double-check that it works
> (hash.hs for an argument of 2000 should produce
> 4999. I get 0 with your
> version).
Fixed.
> >matrix
> >
> >
> This version produces incorrect results for input of
> 100. Perhaps it's
> overflowing?
Incorrect in absoulte, but the same as the previous
version... More on this below.
> >moments
> >
> >
> This doesn't compile for me.
Fixed.
> >regexmatch
> >
> >
> I'm not convinced on this topic yet. Leaving
> existing.
See discussion below...
> >sieve
> >
> >
> I'm leaving the old version, since it seems to force
> GHC to do the extra
> work.
Well, according to your benchmark, it doesn't:
...
gforth 0.02 0.26 0.52 0.78
ghc 0.11 0.11 0.11 0.11
gij 0.11 1.56 3.01 4.46
...
-- shootout.alioth.debian.org/bench/sieve/detail.php
GHC apparently has become so clever that it is very
difficult to trick into doing two times the same
thing.
If at all possible, this requires resorting to ugly
code. Therefore, I suggest to change the benchamrk
specifications so that the "number of times to perform
the action" is reflected in one way or another in the
output.
The author of the matrix benchmark chose to compute
the power matrix instead of doing n times the same
computation, and this explains the discrepancy. For
sieve, I suggest computing more primes, and for
regexmatch, a longer input.
Of course, this requires quite a lot of work (changing
all other implementations). Given that, and in the
light that "optimisation circumvention devices" as
they stand are not satisfactory (don't work at all or
produce wrong output), and (except for matrix) are so
ugly, the alternative is to get rid of them, even if
that means to disqualify ghc for those particular
benchmarks.
> >wc
> >
> >
> I included this implementation as a comment (it's
> beautiful! Just a few
> lines). (But performance is still quite bad!)
Since, for this test, ghc is already last by far in
terms of CPU, I thought it could at least make up for
it in terms of code simplicity.
> >wordfreq
> >
> >
> This fails:
Fixed.
> I'm hesitant to second-guess Simon Marlow on topics
> of a Haskell nature,
> so I'm leaving his version. However, I added your
> more readable version
> as a set of comments to his version.
I didn't mean to second guess him eiher. Yet he
focused on performance, thus the result uses a
convoluted approach. Mentioning the straightforward
way in comment, as you did, is what I suggested :)
As a personal comment and justification of my
proposals... I think the great advantage of haskell is
its clarity and brievty, so I'd rather see it well
placed in the LOC competition than in the CPU one.
Cheers,
JP.
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail
--0-733717733-1088349403=:46778
Content-Type: text/x-haskell; name="hash.hs"
Content-Description: hash.hs
Content-Disposition: inline; filename="hash.hs"
-- $Id: hash.ghc,v 1.1.1.1 2004/05/19 18:09:55 bfulgham Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Bryn Keller
-- modified by JP Bernardy
-- build with ghc -O2 -fglasgow-exts sourcefile.hs
import System (getArgs)
import Data.FiniteMap
import Numeric (showHex)
countKeys tbl n = length [() | i <- [1..n], lookupWithDefaultFM tbl False (show i)]
main = do [number] <- getArgs
let num = read number
tbl = listToFM [(showHex i "", True) | i <- [(1::Int)..num]]
print (countKeys tbl num)
--0-733717733-1088349403=:46778
Content-Type: text/x-haskell; name="moments.hs"
Content-Description: moments.hs
Content-Disposition: inline; filename="moments.hs"
-- $Id: moments.ghc,v 1.1.1.1 2004/05/19 18:10:47 bfulgham Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Brian Gregor
-- with modifications by Max
-- with further modifications by JP Bernardy
import Numeric
import List(sort)
main = interact $ unlines . answers . map read . lines
-- compute out the answers
answers :: [Double] -> [String]
answers nums = ["n: " ++ show (length nums),
"median: " ++ (showFFloat (Just 6) (median nums n) ""),
"mean: " ++ (showFFloat (Just 6) mean ""),
"average_deviation: " ++ (showFFloat (Just 6) avg_dev ""),
"standard_deviation: " ++ (showFFloat (Just 6) std_dev ""),
"variance: " ++ (showFFloat (Just 6) var ""),
"skew: " ++ (showFFloat (Just 6) skew ""),
"kurtosis: " ++ (showFFloat (Just 6) kurt "")]
where n = fromIntegral (length nums)
mean = sum nums / n
deviation = [x-mean | x <- nums]
avg_dev = sum (map abs deviation) / n
var = sum [x**2 | x <- deviation] / (n-1)
std_dev = sqrt var
skew = sum [x**3 | x <- deviation] / (n*var*std_dev)
kurt = sum [x**4 | x <- deviation] / (n*var*var) - 3.0
-- calculate the median
median nums n = mid (sort nums)
where mid x
| odd (length x) = x!! midpt
| otherwise = ((x!!(midpt-1)) + (x!!midpt)) / 2.0
midpt = floor (n/2)
--0-733717733-1088349403=:46778
Content-Type: text/x-haskell; name="wordfreq.hs"
Content-Description: wordfreq.hs
Content-Disposition: inline; filename="wordfreq.hs"
-- $Id: wordfreq.ghc,v 1.1.1.1 2004/05/19 18:14:16 bfulgham Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Julian Assange
-- simplified by JP Bernardy
-- compile with:
-- ghc -O2 wordfreq.hs -o wordfreq
import Char(toLower,isAlpha)
import Data.FiniteMap(fmToList,emptyFM,addToFM_C)
import Data.List(sortBy, foldl')
main = interact $ unlines . map pretty . sortBy myCompare . fmToList . makemap . words . map normalize
where pretty (w,n) = pad 7 (show n) ++ "\t" ++ w
where pad n s = replicate (n - length s) ' ' ++ s
myCompare (w0,n0) (w1,n1) = compare (n1,w1) (n0,w0)
makemap = foldl' addFM emptyFM
where addFM fm x = addToFM_C (+) fm x (1::Int)
normalize x = if isAlpha x then toLower x else ' '
--0-733717733-1088349403=:46778--