簡體   English   中英

Haskell獲取函數運行時間

[英]Haskell get function run time

我如何捕獲Haskell函數的運行時,例如,用GHC編譯的包含Main.hs文件,該函數包含對列表中的項目進行排序的函數“ bubbleSort”:

 bubbleSort :: (Ord t) => [t] -> [t]
 bubbleSort a = loop (length a) bubble a 

 bubble :: (Ord t) => [t] -> [t]
 bubble (a:b:c) | a < b = a : bubble (b:c)
       | otherwise = b : bubble (a:c)
 bubble (a:[]) = [a] 
 bubble [] = []

 loop :: (Num a, Ord a) => a -> (t -> t) -> t -> t
 loop num f x | num > 0 =  loop (num-1) f x'
     | otherwise = x
     where x' = f x 

注意:我知道這不是最有效的排序方法。

您可以嘗試這樣的標准,

import System.Random
import Data.List
import Criterion.Main
import Criterion.Config


bubbleSort :: (Ord t) => [t] -> [t]
bubbleSort a = loop (length a) bubble a 

loop :: (Num a, Ord a) => a -> (t -> t) -> t -> t
loop num f x 
    | num > 0 =  loop (num-1) f x'
    | otherwise = x
        where x' = f x

bubble :: (Ord t) => [t] -> [t]
bubble (a:b:c) | a < b = a : bubble (b:c)
               | otherwise = b : bubble (a:c)
bubble (a:[]) = [a] 
bubble [] = []

randomlist :: Int -> StdGen -> [Int]
randomlist n = take n . unfoldr (Just . random)

main = do 
    seed <- newStdGen 
    let 
        xs100  = randomlist 100  seed  
        xs500  = randomlist 500  seed 
        xs2500 = randomlist 2500 seed 
      in defaultMainWith defaultConfig (return ()) [
            bgroup "bubble" [
                bench "bubble 100"  $ nf bubble xs100
              , bench "bubble 500"  $ nf bubble xs500
              , bench "bubble 2500" $ nf bubble xs2500
              ],
            bgroup "bubble Sort" [
                bench "bubbleSort 100"  $ nf bubbleSort xs100
              , bench "bubbleSort 500"  $ nf bubbleSort xs500
              , bench "bubbleSort 2500" $ nf bubbleSort xs2500
              ]
            ]

然后輸出

warming up
estimating clock resolution...
mean is 2.181457 us (320001 iterations)
found 41466 outliers among 319999 samples (13.0%)
  2428 (0.8%) low severe
  39038 (12.2%) high severe
estimating cost of a clock call...
mean is 105.7764 ns (13 iterations)

benchmarking bubble/bubble 100
mean: 5.174493 us, lb 5.158926 us, ub 5.190592 us, ci 0.950
std dev: 80.64570 ns, lb 70.99540 ns, ub 93.12886 ns, ci 0.950
variance introduced by outliers: 8.479%
variance is slightly inflated by outliers

benchmarking bubble/bubble 500
mean: 28.41568 us, lb 28.22828 us, ub 28.64927 us, ci 0.950
std dev: 1.071815 us, lb 843.6888 ns, ub 1.531296 us, ci 0.950
found 4 outliers among 100 samples (4.0%)
  2 (2.0%) high mild
  1 (1.0%) high severe
variance introduced by outliers: 34.577%
variance is moderately inflated by outliers

benchmarking bubble/bubble 2500
mean: 132.3620 us, lb 131.0149 us, ub 134.1072 us, ci 0.950
std dev: 7.802474 us, lb 6.333342 us, ub 11.58801 us, ci 0.950
found 1 outliers among 100 samples (1.0%)
  1 (1.0%) high severe
variance introduced by outliers: 56.487%
variance is severely inflated by outliers

benchmarking bubble Sort/bubbleSort 100
mean: 399.7690 us, lb 398.7208 us, ub 400.7847 us, ci 0.950
std dev: 5.291009 us, lb 4.761788 us, ub 5.961798 us, ci 0.950
variance introduced by outliers: 6.563%
variance is slightly inflated by outliers

benchmarking bubble Sort/bubbleSort 500
mean: 15.42273 ms, lb 15.26078 ms, ub 15.60196 ms, ci 0.950
std dev: 872.8984 us, lb 784.0365 us, ub 967.8269 us, ci 0.950
variance introduced by outliers: 54.470%
variance is severely inflated by outliers

benchmarking bubble Sort/bubbleSort 2500
collecting 100 samples, 1 iterations each, in estimated 48.56091 s
mean: 473.5322 ms, lb 472.0005 ms, ub 474.9877 ms, ci 0.950
std dev: 7.695022 ms, lb 6.700990 ms, ub 9.001423 ms, ci 0.950
found 2 outliers among 100 samples (2.0%)
  2 (2.0%) low mild
variance introduced by outliers: 9.408%
variance is slightly inflated by outliers

最簡單,最不復雜的方法是,首先使用ghc --make yourfile.hs編譯文件,然后在shell 命令提示符下將其運行為> yourfile +RTS -s然后檢查統計信息打印輸出。

該文件應以

{-# OPTIONS_GHC -O2 #-}

module Main 
where

並且包含一個類型為IO () main值,例如

main :: IO ()
main = print $ bubbleSort ([1..50]++[42])

為了從中獲得有意義的理解,您應該找出算法增長經驗順序 只是一個數據點並不能告訴您太多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM