I am trying the following code to check time differences between function calls:
module Main where
import Control.Concurrent
import Data.Time.Clock
printTimeDiff :: UTCTime -> IO ()
printTimeDiff t0 = do
t <- getCurrentTime
let tt = realToFrac $ diffUTCTime t t0
print (tt :: Double)
-- threadDelay 1
printTimeDiff t
main :: IO ()
main = do
t <- getCurrentTime
printTimeDiff t
It prints zeros in 70%:
0.0
0.0
0.0
0.0
0.0
0.0
1.0e-3
1.0032e-3
9.994e-4
0.0
0.0
0.0
0.0
0.0
0.0
9.984e-4
0.0
0.0
0.0
0.0
0.0
0.0
0.0
9.993e-4
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
1.0007e-3
0.0
0.0
0.0
0.0
If I uncomment the line threadDelay 1
- it print real time differences.
Why is that?
Is it caused by thread manager running a few IO actions simultaneously? If so - how could I prevent it?
Or it relates to buffering used by print
?
Probably getCurrentTime
is accessing a clock that's too imprecise for the speeds your code is running at. Consider using one of the higher-resolution timers, using e.g. the clock package.