2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1997
4 \section[CPUTime]{Haskell 1.4 CPU Time Library}
7 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
11 getCPUTime, -- :: IO Integer
12 cpuTimePrecision -- :: Integer
19 import PrelArr ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
28 import PrelNum ( Num(..), Integral(..) ) -- To get fromInt/toInt
33 Computation @getCPUTime@ returns the number of picoseconds CPU time
34 used by the current program. The precision of this result is
35 implementation-dependent.
37 The @cpuTimePrecision@ constant is the smallest measurable difference
38 in CPU time that the implementation can record, and is given as an
39 integral number of picoseconds.
44 getCPUTime :: IO Integer
46 marr <- primNewByteArray (sizeof_int * 4)
47 ptr <- getCPUTime marr
48 if (ptr /= nullAddr) then do
49 x0 <- primReadIntArray marr 0
50 x1 <- primReadIntArray marr 1
51 x2 <- primReadIntArray marr 2
52 x3 <- primReadIntArray marr 3
53 return ((fromIntegral x0 * 1000000000 + fromIntegral x1 +
54 fromIntegral x2 * 1000000000 + fromIntegral x3)
57 ioError (IOError Nothing UnsupportedOperation
63 getCPUTime :: IO Integer
65 stToIO (newIntArray ((0::Int),3)) >>= \ marr ->
66 stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ frozen#) ->
67 primGetCPUTime barr >>= \ ptr ->
68 if (ptr::Addr) /= nullAddr then
69 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
70 fromIntegral (I# (indexIntArray# frozen# 1#)) +
71 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
72 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
74 ioError (IOError Nothing UnsupportedOperation
80 cpuTimePrecision :: Integer
81 cpuTimePrecision = round ((1000000000000::Integer) %
82 fromInt (unsafePerformIO clockTicks))
89 foreign import "libHS_cbits" "getCPUTime" primGetCPUTime :: ByteArray Int -> IO Addr
90 foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int