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
21 import PrelArr ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
28 import PrelNum ( Num(..), Integral(..) ) -- To get fromInt/toInt
32 Computation @getCPUTime@ returns the number of picoseconds CPU time
33 used by the current program. The precision of this result is
34 implementation-dependent.
36 The @cpuTimePrecision@ constant is the smallest measurable difference
37 in CPU time that the implementation can record, and is given as an
38 integral number of picoseconds.
41 getCPUTime :: IO Integer
43 marr <- stToIO (newIntArray ((0::Int),3))
44 barr <- stToIO (unsafeFreezeByteArray marr)
45 rc <- primGetCPUTime barr
48 ByteArray _ _ frozen# -> -- avoid bounds checking
49 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
50 fromIntegral (I# (indexIntArray# frozen# 1#)) +
51 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
52 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
54 ioError (IOError Nothing UnsupportedOperation
58 cpuTimePrecision :: Integer
59 cpuTimePrecision = round ((1000000000000::Integer) %
60 fromInt (unsafePerformIO clockTicks))
62 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
63 foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
70 getCPUTime :: IO Integer
72 = do seconds <- nh_getCPUtime
73 return (round (seconds * 1.0e+12))
75 cpuTimePrecision :: Integer
78 do resolution <- nh_getCPUprec
79 return (round (resolution * 1.0e+12))