Timing Functions
Lemmih
lemmih at gmail.com
Mon Jan 17 12:32:15 EST 2005
On Mon, 17 Jan 2005 10:48:18 -0600, jekwtw <jeaniek7 at comcast.net> wrote:
>
> I'm putting together a script to gather run-time stats for some functions
> I'm working with, and I'm having a terrible time. My strategy is to
> evaluate a function a number of times and compute the difference between the
> elapsed CPU time before and after the repeated calls.
>
> > timeNReps :: (a -> b) -> a -> Int -> FilePath -> IO ()
> > timeNReps func arg reps fileName =
> > do t0 <- System.CPUTime.getCPUTime
> > runNReps func arg reps
> > t1 <- System.CPUTime.getCPUTime
> > appendFile fileName ((showMS (t1 - t0)) ++ "\n")
> > where
> > showMS n = show (n `quot` 1000000000)
>
> showMS just converts the pico-second result into milli-seconds and
> stringifies it.
>
> runNReps is an IO program (do sequence) that is intended to call the
> function and tail-call itself a given number of times:
>
> > runNReps :: (Int -> a) -> Int -> Int -> IO ()
> > runNReps f x todo
> > | todo > 0 = do let junk = (f x)
> > runNReps f x (todo - 1)
> > | otherwise = return (())
Haskell is a non-strict language which means that 'junk' wont be
evaluated since it's not necessary for the function to terminate.
Check 'replicateM_' from Control.Monad.
> runNReps :: Int -> IO a -> IO ()
> runNReps = replicateM_
--
Friendly,
Lemmih
More information about the Glasgow-haskell-users
mailing list