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, unsafeFreezeByteArray )
24 import PrelNum ( fromInt )
25 import PrelIOBase ( IOError(..), IOErrorType( UnsupportedOperation ),
26 unsafePerformIO, stToIO )
30 Computation @getCPUTime@ returns the number of picoseconds CPU time
31 used by the current program. The precision of this result is
32 implementation-dependent.
34 The @cpuTimePrecision@ constant is the smallest measurable difference
35 in CPU time that the implementation can record, and is given as an
36 integral number of picoseconds.
39 getCPUTime :: IO Integer
41 marr <- stToIO (newIntArray ((0::Int),3))
42 barr <- stToIO (unsafeFreezeByteArray marr)
43 rc <- primGetCPUTime barr
46 ByteArray _ _ frozen# -> -- avoid bounds checking
47 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
48 fromIntegral (I# (indexIntArray# frozen# 1#)) +
49 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
50 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
52 ioError (IOError Nothing UnsupportedOperation
56 cpuTimePrecision :: Integer
57 cpuTimePrecision = round ((1000000000000::Integer) %
58 fromInt (unsafePerformIO clockTicks))
60 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
61 foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
69 import privileged Prelude ( nh_getCPUtime
74 getCPUTime :: IO Integer
76 = do seconds <- nh_getCPUtime
77 return (round (seconds * 1.0e+12))
79 cpuTimePrecision :: Integer
82 do resolution <- nh_getCPUprec
83 return (round (resolution * 1.0e+12))