[Haskell-cafe] First call to putStr takes far more time than subsequent calls

Ben Gunton ben.gunton at gmail.com
Thu Jul 9 20:17:50 UTC 2015


This is a simplified version of an issue I'm running into when timing some
functions.

In this example, the first putStr inside the replicateM_ takes far longer
than subsequent putStrs. If I add in a putStr at the top (the commented
line), each iteration takes the same. (Though I presume I just shifted the
long execution to the putStr on top).

The same thing happens on the first iterations of a function when I need to
evaluate some state, or deepseq some value. putStr was just a simpler
version that still showed the same issue.

Relevant code: http://lpaste.net/136250

What's going on here?

Thanks!

import Control.Monadimport Data.Time.Clock
main :: IO ()main = do
    -- putStr ""
    replicateM_ 5 $ do
        t1 <- getCurrentTime
        putStr ""
        t2 <- getCurrentTime
        print . round $ 1000 * 1000 * diffUTCTime t2 t1
{-
Sample output:
49
1
1
1
2
-}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150709/bf9bde12/attachment.html>


More information about the Haskell-Cafe mailing list