1 % -----------------------------------------------------------------------------
2 % $Id: CPUTime.lhs,v 1.25 2000/09/14 13:46:42 simonpj Exp $
4 % (c) The University of Glasgow, 1995-2000
6 \section[CPUTime]{Haskell 98 CPU Time Library}
9 {-# OPTIONS -#include "cbits/stgio.h" #-}
13 getCPUTime, -- :: IO Integer
14 cpuTimePrecision -- :: Integer
22 import Prelude -- To generate the dependency
23 import PrelGHC ( indexIntArray# )
24 import PrelBase ( Int(..) )
25 import PrelByteArr ( ByteArray(..), newIntArray )
26 import PrelArrExtra ( unsafeFreezeByteArray )
27 import PrelNum ( fromInt )
28 import PrelIOBase ( IOError, IOException(..),
29 IOErrorType( UnsupportedOperation ),
30 unsafePerformIO, stToIO, ioException )
34 Computation @getCPUTime@ returns the number of picoseconds CPU time
35 used by the current program. The precision of this result is
36 implementation-dependent.
38 The @cpuTimePrecision@ constant is the smallest measurable difference
39 in CPU time that the implementation can record, and is given as an
40 integral number of picoseconds.
43 getCPUTime :: IO Integer
45 marr <- stToIO (newIntArray ((0::Int),3))
46 barr <- stToIO (unsafeFreezeByteArray marr)
47 rc <- primGetCPUTime barr
50 ByteArray _ _ frozen# -> -- avoid bounds checking
51 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
52 fromIntegral (I# (indexIntArray# frozen# 1#)) +
53 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
54 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
56 ioException (IOError Nothing UnsupportedOperation
60 cpuTimePrecision :: Integer
61 cpuTimePrecision = round ((1000000000000::Integer) %
62 fromInt (unsafePerformIO clockTicks))
64 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
65 foreign import "libHS_cbits" "clockTicks" unsafe clockTicks :: IO Int
72 import PrelPrim ( nh_getCPUtime
77 getCPUTime :: IO Integer
79 = do seconds <- nh_getCPUtime
80 return (round (seconds * 1.0e+12))
82 cpuTimePrecision :: Integer
85 do resolution <- nh_getCPUprec
86 return (round (resolution * 1.0e+12))