[Haskell-cafe] getCPUTime ??

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon Nov 21 16:35:50 EST 2005


On Mon, 2005-11-21 at 15:14 -0500, Michael Benfield wrote:
> I'm new to Haskell. I'm apparently misunderstanding something here.
> 
> When I run this program:
> 
> ---------
> module Main where
> 
> import System.Posix
> import System.CPUTime
> 
> printTime = getCPUTime >>= putStrLn . show
> 
> main = printTime >> sleep 5 >> printTime
> ---------
> 
> It produces this output:
> 1430000000000
> 1430000000000
> 
> or similar. In any case, both the numbers are the same. Should the 
> second number not reflect a time 5 seconds later than the first? I've 
> tried this with both GHC and Hugs, and both give me the same thing.

http://haskell.org/ghc/docs/latest/html/libraries/base/System-CPUTime.html

getCPUTime :: IO Integer
        Computation getCPUTime returns the number of picoseconds CPU
        time used by the current program. The precision of this result
        is implementation-dependent. 

The key point there is that it is the CPU time *used* by the program. So
sleeping uses no cpu time. Also, the precision is probably not terribly
high which is why you get the exact same answer despite having used a
minute about of actual cpu time. You can find the precision by using
cpuTimePrecision.

You probably want actual time rather than cpu time, in which case you
should use the System.Time module.

Duncan



More information about the Haskell-Cafe mailing list