Performance Tests in Haskell

David Roundy droundy at abridgegame.org
Thu Oct 16 08:57:26 EDT 2003


On Thu, Oct 16, 2003 at 11:26:11AM +0200, Andreas.Schroeder at gillardon.de wrote:
> 1. how can i do performance tests if the tests are small? In imperative
> languages, you cycle through 1000 times and divide the runtime by 1000.
>     How can i do something similar  in haskell?

Even in an imperative language, running a fast calculation in a loop is a
fishy way to run a timing.  I'd say that whatever sort of language you're
using, the best way to run a timing is always in the context of how you
plan to actually use the routine.  If it's so fast that you can't
distinguish its time from that spent in the calling routine, it means you
don't need to bother optimizing it.

That being said, you you can pretty easily use map and fold to run a
function a number of times and actually used.  Whether you're using a
functional language or an imperative one, in the presense of sufficiently
smart compilers you always have to make sure your result is actually used
in some way--otherwise the compiler could just skip the loop.  In a lazy
language like haskell, even if the compiler isn't smart, if the results
aren't used it'll probably skip the loop.

slow_function :: Double -> Double

test max = sum $ map slow_function [0.0,0.001..max]

> 2. How do i get haskell to do something _without_ printing anything on the
> screen? For now, i do something like
> 
> main = do
>     startt <- getCPUTime
>     putStr $ (show testAIBD1) ++ ", " ++ (show testZins1)
>     endt <- getCPUTime
>     putStr $ "\tTook " ++ (show $ (endt - startt) `div` ((10^6) * 5)) ++ "
> ns\n"
> 
> where testAIBD1 is a sum of 40 calculation results and also is testZins1.

main = do
    startt <- getCPUTime
    when (testAIBD1 == a value) $ exitWith ExitSuccess
    when (testZins1 == a value) $ exitWith ExitSuccess
    endt <- getCPUTime
    putStr $ "\tTook " ++ (show $ (endt - startt) `div` ((10^6) * 5)) ++ "ns\n"

Except that you probably need to give an input via the IO monad to
testAIBD1, otherwise the compiler can determine at compile time that the
equality is false and not bother generating any code for it.
-- 
David Roundy
http://www.abridgegame.org/darcs


More information about the Glasgow-haskell-users mailing list