我试图编写代码来计算函数花费的时间
list <- buildlist 10000 10000
starttime <- getClockTime
let sortedlist = quicksort list
endtime <- getClockTime
let difftime = diffClockTimes endtime starttime
功能构建列表:
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 [] = []
quicksort (x:xs) =
let head = [a|a<-xs,a<=x]
tail = [a|a<-xs,a>x]
in quicksort head ++ [x] ++ quicksort tail
第一个问题:当我输出 difftime 时,无论列表有多长,它总是为零。
第二个:我想知道Real World Haskell代码中的“>>=”是什么意思。
getClockTime >>= (\(TOD sec _) -> return sec)
第三:我写这个是为了从TimeDiff变量中获取tdSec和tdPicosec 。有没有更简单的方法?
time <-(\(TimeDiff _ _ _ _ _ s ps) -> return [ ( \a -> fromIntegral a :: Double ) s , ( \a -> fromIntegral a :: Double ) ps ] ) difftime