[project @ 1997-05-18 04:59:19 by sof]
[ghc-hetmet.git] / ghc / lib / required / CPUTime.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1997
3 %
4 \section[CPUTime]{Haskell 1.4 CPU Time Library}
5
6 \begin{code}
7 module CPUTime 
8         (
9          getCPUTime,       -- :: IO Integer
10          cpuTimePrecision  -- :: Integer
11         ) where
12
13 import PrelBase ( Int(..), indexIntArray# )
14 import ArrBase  ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
15 import Foreign  ( Addr )
16 import IOBase
17 import IO
18 import STBase
19 import Ratio
20
21 \end{code}
22
23 Computation @getCPUTime@ returns the number of picoseconds CPU time
24 used by the current program.  The precision of this result is
25 implementation-dependent.
26
27 The @cpuTimePrecision@ constant is the resolution (in picoseconds!) of
28 the number of 
29
30 \begin{code}
31 getCPUTime :: IO Integer
32 getCPUTime =
33     newIntArray (0,3)                       `thenIO_Prim` \ marr ->
34     unsafeFreezeByteArray marr              `thenIO_Prim` \ barr@(ByteArray _ frozen#) ->
35     _ccall_ getCPUTime barr                 `thenIO_Prim` \ ptr ->
36     if (ptr::Addr) /= ``NULL'' then
37         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
38                 fromIntegral (I# (indexIntArray# frozen# 1#)) + 
39                 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
40                 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
41     else
42         fail (IOError Nothing UnsupportedOperation "getCPUTime: can't get CPU time")
43
44 cpuTimePrecision :: Integer
45 cpuTimePrecision = round ((1000000000000::Integer) % 
46                           fromInt (unsafePerformPrimIO (_ccall_ clockTicks )))
47 \end{code}
48
49
50
51
52