2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1997
4 \section[CPUTime]{Haskell 1.4 CPU Time Library}
7 {-# OPTIONS -#include "cbits/stgio.h" #-}
11 getCPUTime, -- :: IO Integer
12 cpuTimePrecision -- :: Integer
20 import Prelude -- To generate the dependency
21 import PrelGHC ( indexIntArray# )
22 import PrelBase ( Int(..) )
23 import PrelByteArr ( ByteArray(..), newIntArray )
24 import PrelArrExtra ( unsafeFreezeByteArray )
25 import PrelNum ( fromInt )
26 import PrelIOBase ( IOError(..), IOErrorType( UnsupportedOperation ),
27 unsafePerformIO, stToIO )
31 Computation @getCPUTime@ returns the number of picoseconds CPU time
32 used by the current program. The precision of this result is
33 implementation-dependent.
35 The @cpuTimePrecision@ constant is the smallest measurable difference
36 in CPU time that the implementation can record, and is given as an
37 integral number of picoseconds.
40 getCPUTime :: IO Integer
42 marr <- stToIO (newIntArray ((0::Int),3))
43 barr <- stToIO (unsafeFreezeByteArray marr)
44 rc <- primGetCPUTime barr
47 ByteArray _ _ frozen# -> -- avoid bounds checking
48 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
49 fromIntegral (I# (indexIntArray# frozen# 1#)) +
50 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
51 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
53 ioError (IOError Nothing UnsupportedOperation
57 cpuTimePrecision :: Integer
58 cpuTimePrecision = round ((1000000000000::Integer) %
59 fromInt (unsafePerformIO clockTicks))
61 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
62 foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
70 import privileged Prelude ( nh_getCPUtime
75 getCPUTime :: IO Integer
77 = do seconds <- nh_getCPUtime
78 return (round (seconds * 1.0e+12))
80 cpuTimePrecision :: Integer
83 do resolution <- nh_getCPUprec
84 return (round (resolution * 1.0e+12))