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 )
27 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.
43 getCPUTime :: IO Integer
45 marr <- primNewByteArray (sizeof_int * 4)
48 x0 <- primReadIntArray marr 0
49 x1 <- primReadIntArray marr 1
50 x2 <- primReadIntArray marr 2
51 x3 <- primReadIntArray marr 3
52 return ((fromIntegral x0 * 1000000000 + fromIntegral x1 +
53 fromIntegral x2 * 1000000000 + fromIntegral x3)
56 ioError (IOError Nothing UnsupportedOperation
62 getCPUTime :: IO Integer
64 stToIO (newIntArray ((0::Int),3)) >>= \ marr ->
65 stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ _ frozen#) ->
66 primGetCPUTime barr >>= \ rc ->
68 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
69 fromIntegral (I# (indexIntArray# frozen# 1#)) +
70 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
71 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
73 ioError (IOError Nothing UnsupportedOperation
79 cpuTimePrecision :: Integer
80 cpuTimePrecision = round ((1000000000000::Integer) %
81 fromInt (unsafePerformIO clockTicks))
88 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
89 foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int