Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance Haskell get函数运行时_Performance_Haskell - Fatal编程技术网

Performance Haskell get函数运行时

Performance Haskell get函数运行时,performance,haskell,Performance,Haskell,如何捕获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)

如何捕获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 
bubbleSort::(Ord t)=>[t]->[t]
bubbleSort a=循环(长度a)气泡a
气泡::(Ord t)=>[t]->[t]
气泡(a:b:c)| aa->(t->t)->t->t
循环数fx | num>0=循环(num-1)fx'
|否则=x
其中x'=fx

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

你可以试试这样的标准

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
              ]
            ]

你可以这样试试

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
              ]
            ]

最简单、最不复杂的方法是,首先用
ghc编译文件——生成yourfile.hs
,然后在shell⁄命令提示符下以
>yourfile+RTS-s
的形式运行它,并检查打印的统计数据

该文件应以开头

{-# OPTIONS_GHC -O2 #-}

module Main 
where
并包含一个类型为
IO()
main

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

要从中获得任何有意义的阅读,你应该找出你的算法的基本原理;只有一个数据点不会告诉你太多。

最简单、最不复杂的方法是,首先用
ghc编译你的文件——生成你的文件.hs
,然后在shell⁄命令提示符下运行它,作为
>yourfile+RTS-s
,并检查打印的统计数据

该文件应以开头

{-# OPTIONS_GHC -O2 #-}

module Main 
where
并包含一个类型为
IO()
main

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

要从中获得任何有意义的阅读,你应该找出你的算法的基本原理;只有一个数据点不会告诉你太多。

有没有办法让它进入更详细的时间测量?。例如,如果时间小于1秒?@user2214957,您可以运行它10或100次,并通过shell(不带-RTS+s)计时;或者增加数据的大小以实现更长的执行时间。这就是我所说的“最不复杂”的意思…)有没有办法让它进入更详细的时间测量?。例如,如果时间小于1秒?@user2214957,您可以运行它10或100次,并通过shell(不带-RTS+s)计时;或者增加数据的大小以实现更长的执行时间。这就是我所说的“最不复杂”的意思…)对于
bubbleSort
,确实
whnf
就足够了。但对于
气泡
,它只强制进行一次比较(可能是交换)。通常,当使用列表结果对函数进行基准测试时,您需要
nf
。为什么“在估计的29.32801 s内收集100个样本,每个样本1次迭代”只在“基准测试气泡排序/bubbleSort 2500”下出现一次?奇怪…@Willenss其他人都很快,Criteria只说如果估计的时间足够长(不记得到底有多长,3秒或5秒,诸如此类)。现在唯一缺少的是
map(logBase 5)[257.38/8.837,8.837/0.2085]
=>
[2.095,2.328]
。顺便问一下,你能控制跑步次数吗?2500箱不需要100次运行;20个就够了;对于5000,我可以选择10,即使是以增加偏差为代价。可能是因为在另一种情况下,计算太短,无法计算出正确的全局运行估计@Daniel我打算用nf更新我的帖子,因为在重新运行后,
bubbleSort
的结果更加一致,事实上,
whnf
就足够了。但对于
气泡
,它只强制进行一次比较(可能是交换)。通常,当使用列表结果对函数进行基准测试时,您需要
nf
。为什么“在估计的29.32801 s内收集100个样本,每个样本1次迭代”只在“基准测试气泡排序/bubbleSort 2500”下出现一次?奇怪…@Willenss其他人都很快,Criteria只说如果估计的时间足够长(不记得到底有多长,3秒或5秒,诸如此类)。现在唯一缺少的是
map(logBase 5)[257.38/8.837,8.837/0.2085]
=>
[2.095,2.328]
。顺便问一下,你能控制跑步次数吗?2500箱不需要100次运行;20个就够了;对于5000,我可以选择10,即使是以增加偏差为代价。可能是因为在另一种情况下,计算太短,无法计算出正确的全局运行估计@Daniel我将用nf更新我的帖子,因为在重新运行后,结果更加一致