Haskell calculates function execution time

I tried to encode to calculate the time during which the function stands

list <- buildlist 10000 10000    
starttime <- getClockTime
let sortedlist = quicksort list
endtime <- getClockTime
let difftime = diffClockTimes endtime starttime

feature list:

buildlist :: Int -> Int -> IO [Int]
buildlist n m = do
    seed <- getStdGen
    let l = randomRs (0, m) seed
    let list = take n l
    return list

quicksort function:

quicksort [] = []
quicksort (x:xs) =
    let head = [a|a<-xs,a<=x]
        tail = [a|a<-xs,a>x]
    in quicksort head ++ [x] ++ quicksort tail

first question: when I output diffftime, it is always zero, regardless of how long this list is.

second: Interestingly, "→ =" in the code from Real World Haskell means.

getClockTime >>= (\(TOD sec _) -> return sec)

Third: I am writing this to get tdSec and tdPicosec from the TimeDiff variable . is there an easier way?

time <-(\(TimeDiff _ _ _ _ _ s ps) -> return [ ( \a -> fromIntegral a :: Double ) s , ( \a -> fromIntegral a :: Double ) ps ] ) difftime
+5
source share
3 answers

Question 1:

! sortedlist quicksort list, , . . .

( ), .

  • seq. seq a -> b -> b , " ". , , deepseq
  • , criterion ( )

2:

>>= . IO IO a a -> IO b IO b. , . foo >>= \x -> expr - ,

 do x <- foo
    expr

, , do - >>=

, 3 - , Stackoverflow.

+11

difftime , Haskell . sortedlist, .

, sortedlist , deepseq Control.Deepseq. deepseq v id, , v.

starttime <- getClockTime
let sortedlist = quicksort list
endtime <- deepseq sortedlist getClockTime

, , TimeDiff. Data getter. , tdSec td td tdPicosec td .

+4

Chronograph: http://hackage.haskell.org/package/chronograph

, :

Prelude Data.Chronograph> :m Data.Chronograph Data.List
Prelude Data.Chronograph Data.List> let theList = reverse [1..1000]
Prelude Data.Chronograph Data.List> sum theList 
500500
Prelude Data.Chronograph Data.List> let timed = chronoNF (sort theList)
Prelude Data.Chronograph Data.List> measure timed
0.000062s
Prelude Data.Chronograph Data.List> head $ val timed
1

:

  • theList, . , , chronoNF
  • chronoNF , deepseq, . . , , .

IO, , , , , IO.

+3

All Articles