I have the following C code:
#include <sys/times.h>
#include <time.h>
float etime_( float *tarray )
{ struct tms buf;
times( &buf );
tarray[0] = 1.0 * buf.tms_utime / CLOCKS_PER_SEC;
tarray[1] = 1.0 * buf.tms_stime / CLOCKS_PER_SEC;
return tarray[0] + tarray[1];
}
Trying to port this Fortran code to Haskell:
PROGRAM Test
IMPLICIT NONE
REAL t, ta(2), etime
INTEGER i
DOUBLE PRECISION x
do i = 1, 10000
x = sin( cos( i * 1.0 d0 ) )
print *, x
enddo
ta(1) = 0.0d0
ta(2) = 0.0d0
t = etime( ta )
PRINT *, 'user time: ', ta(1)
PRINT *, 'system time: ', ta(2)
PRINT *, 'process time: ', t
END
How can I define array and ! or !!! for the below code to work?
module Main where
import GHC.Ptr
import GHC.Prim
import System.IO.Unsafe
import Control.Monad
foreign import ccall etime_ :: Ptr Double → IO Double
etime = etime_
main :: IO Int
main = do
mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
ta ← array 2
t ← etime ta
putStrLn $ "user time: " ++ show (ta !!! 0)
putStrLn $ "system time: " ++ show (ta !!! 1)
putStrLn $ "process time: " ++ show t
return 0
array :: Int → IO (Ptr a)
array size = undefined
(!) :: Ptr a → Int → IO a
(!) = undefined
(!!!) :: Ptr a → Int → a
(!!!) = undefined