577da56c5eaa8d10d34fab7e1a691fb006af5c2c
[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 -#include "cbits/stgio.h" #-}
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 PrelST
23 import Ratio
24
25 \end{code}
26
27 Computation @getCPUTime@ returns the number of picoseconds CPU time
28 used by the current program.  The precision of this result is
29 implementation-dependent.
30
31 The @cpuTimePrecision@ constant is the resolution (in picoseconds!) of
32 the number of 
33
34 \begin{code}
35 getCPUTime :: IO Integer
36 getCPUTime = 
37     stToIO (newIntArray (0,3))          >>= \ marr ->
38     stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ frozen#) ->
39     _ccall_ getCPUTime barr             >>= \ ptr ->
40     if (ptr::Addr) /= ``NULL'' then
41         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
42                 fromIntegral (I# (indexIntArray# frozen# 1#)) + 
43                 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
44                 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
45     else
46         fail (IOError Nothing UnsupportedOperation "getCPUTime"
47                 "can't get CPU time")
48
49 cpuTimePrecision :: Integer
50 cpuTimePrecision = round ((1000000000000::Integer) % 
51                           fromInt (unsafePerformIO (_ccall_ clockTicks )))
52 \end{code}
53
54
55
56
57