1 % -----------------------------------------------------------------------------
2 % $Id: CPUTime.lhs,v 1.29 2001/02/22 16:48:24 qrczak Exp $
4 % (c) The University of Glasgow, 1995-2000
6 \section[CPUTime]{Haskell 98 CPU Time Library}
9 {-# OPTIONS -#include "cbits/stgio.h" #-}
13 getCPUTime, -- :: IO Integer
14 cpuTimePrecision -- :: Integer
22 import Prelude -- To generate the dependency
23 import PrelGHC ( indexIntArray# )
24 import PrelBase ( Int(..) )
25 import PrelByteArr ( ByteArray(..), newIntArray )
26 import PrelArrExtra ( unsafeFreezeByteArray )
27 import PrelIOBase ( IOException(..),
28 IOErrorType( UnsupportedOperation ),
29 unsafePerformIO, stToIO, ioException )
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.
42 getCPUTime :: IO Integer
44 marr <- stToIO (newIntArray ((0::Int),3))
45 barr <- stToIO (unsafeFreezeByteArray marr)
46 rc <- primGetCPUTime barr
49 ByteArray _ _ frozen# -> -- avoid bounds checking
50 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
51 fromIntegral (I# (indexIntArray# frozen# 1#)) +
52 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
53 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
55 ioException (IOError Nothing UnsupportedOperation
60 cpuTimePrecision :: Integer
61 cpuTimePrecision = round ((1000000000000::Integer) %
62 fromIntegral (unsafePerformIO clockTicks))
64 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
65 foreign import "libHS_cbits" "clockTicks" unsafe clockTicks :: IO Int
72 import PrelPrim ( nh_getCPUtime
77 getCPUTime :: IO Integer
79 = do seconds <- nh_getCPUtime
80 return (round (seconds * 1.0e+12))
82 cpuTimePrecision :: Integer
85 do resolution <- nh_getCPUprec
86 return (round (resolution * 1.0e+12))