[project @ 1998-02-02 17:27:26 by simonm]
[ghc-hetmet.git] / ghc / lib / std / 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 {-# OPTIONS -fno-implicit-prelude #-}
8
9 module CPUTime 
10         (
11          getCPUTime,       -- :: IO Integer
12          cpuTimePrecision  -- :: Integer
13         ) where
14
15 import PrelBase
16 import PrelArr  ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
17 import PrelMaybe
18 import PrelNum
19 import PrelAddr
20 import PrelIOBase
21 import IO
22 import PrelUnsafe   ( unsafePerformIO )
23 import PrelST
24 import Ratio
25
26 \end{code}
27
28 Computation @getCPUTime@ returns the number of picoseconds CPU time
29 used by the current program.  The precision of this result is
30 implementation-dependent.
31
32 The @cpuTimePrecision@ constant is the resolution (in picoseconds!) of
33 the number of 
34
35 \begin{code}
36 getCPUTime :: IO Integer
37 getCPUTime = 
38     stToIO (newIntArray (0,3))          >>= \ marr ->
39     stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ frozen#) ->
40     _ccall_ getCPUTime barr             >>= \ ptr ->
41     if (ptr::Addr) /= ``NULL'' then
42         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
43                 fromIntegral (I# (indexIntArray# frozen# 1#)) + 
44                 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
45                 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
46     else
47         fail (IOError Nothing UnsupportedOperation 
48                 "getCPUTime: can't get CPU time")
49
50 cpuTimePrecision :: Integer
51 cpuTimePrecision = round ((1000000000000::Integer) % 
52                           fromInt (unsafePerformIO (_ccall_ clockTicks )))
53 \end{code}
54
55
56
57
58