[Haskell-cafe] Timing pure functions?
magnus at therning.org
Wed May 27 05:12:10 EDT 2009
On Wed, May 27, 2009 at 9:59 AM, Andrew Butterfield
<Andrew.Butterfield at cs.tcd.ie> wrote:
> Magnus Therning wrote:
>> timeIt times ioa = let
>> timeOnce = do
>> t1 <- getCPUTime
>> a <- ioa
>> t2 <- getCPUTime
>> let t = fromIntegral (t2-t1) * 1e-12
>> return t
>> in sequence $ take times $ repeat timeOnce
>> main = do
>> fh <- openBinaryFile "/dev/urandom" ReadMode
>> d <- liftM BS.unpack $ BS.hGet fh 100000
>> t <- timeIt 10 $ return $! B64.encode d
>> print t
>> I suspect that it all comes from `B64.encode d` being pure, hence the
>> encoding happens only once. Now I _really_ want the encoding to
>> happen 10 times, is there some easy way to achieve this?
> A quick answer - not a lot of thought - pass function *and* argument
> separately into timeIt ?
> timeIt times ioaf ioaarg
> .... a <- ioaf ioaarg
> As it stands you pass the thunk (B64.encode d) in so it only gets evaluated
> If you pass the function and argument in then a new thunk is built each time
> (unless the optimiser nabbles it.......)
Hmm, my naive implementation of that didn't improve the situation, `t
<- timeIt 10 (\ x -> return $! B64.encode x) d` still results in only
one measurement /= 0.
Of course that also makes `timeIt` less general.
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnus＠therning．org Jabber: magnus＠therning．org
http://therning.org/magnus identi.ca|twitter: magthe
More information about the Haskell-Cafe