【问题标题】:Is it possible to profiling space usage in Haskell是否可以在 Haskell 中分析空间使用情况
【发布时间】:2012-09-07 17:19:24
【问题描述】:

Lecture chapter 12 by Erik 通过引入示例函数sumWith 提及“提高空间使用率”。

我同时代表惰性版本和严格版本,如下所示。

sumWith1 v [] = v
sumWith1 v (x:xs) = sumWith1 (v+x) xs

sumWith2 v [] = v
sumWith2 v (x:xs) = (sumWith2 $! (v+x)) xs

test = sumWith1 0 [1..200000000]

我想严格的版本应该会在一定程度上提高性能 我尝试验证杠杆 GHC 分析工具。

$ ghc --make -O2 -prof -auto-all -rtsopts -o test1
$ ./test1 +RTS -p -RTS

test 函数中将sumWith1 更改为sumWith2 并作为test2 再次执行。

这是分析结果:http://pastie.org/4720019

通过查看%alloc 列,我看不出这两个函数之间有任何区别。

我的问题是如何改进测试用例以找到一些差异。 换句话说,是否可以分析这种情况下的空间使用情况?

谢谢。

【问题讨论】:

  • 你有没有试过在编译没有优化之后比较它们?
  • 经过优化,当test 使用sumWith1 时,GHC 会生成一个专门的([Integer] -> Integer 类型)版本,由于严格分析器知道添加Integers 是严格的,生成的代码与使用时显式严格的 sumWith2 得到的代码相同。

标签: haskell


【解决方案1】:

使用 GHC 的堆分析器。

RWH, ch25中有详细描述。这里是a fully worked example

【讨论】:

    【解决方案2】:

    这是你程序的统计数据(我已将上限降低到 1000000)

    这是你稍微修改过的程序

    sumWith1 v [] = v
    sumWith1 v (x:xs) = sumWith1 (v+x) xs
    
    sumWith2 v [] = v
    sumWith2 v (x:xs) = (sumWith2 $! (v+x)) xs
    
    main = print $ sumWith1 0 [1..1000000]
    

    ghc -prof -fprof-auto -rtsopts heap.hs编译

    对于 sumwith1

    ./heap +RTS -sstderr -K500M
    500000500000
         266,384,496 bytes allocated in the heap
         367,442,520 bytes copied during GC
         117,747,616 bytes maximum residency (8 sample(s))
           1,931,472 bytes maximum slop
                 196 MB total memory in use (0 MB lost due to fragmentation)
    
                                        Tot time (elapsed)  Avg pause  Max pause
      Gen  0       393 colls,     0 par    0.19s    0.19s     0.0005s    0.0455s
      Gen  1         8 colls,     0 par    0.20s    0.21s     0.0257s    0.0832s
    
      INIT    time    0.00s  (  0.00s elapsed)
      MUT     time    0.15s  (  0.16s elapsed)
      GC      time    0.39s  (  0.39s elapsed)
      RP      time    0.00s  (  0.00s elapsed)
      PROF    time    0.00s  (  0.00s elapsed)
      EXIT    time    0.00s  (  0.00s elapsed)
      Total   time    0.55s  (  0.55s elapsed)
    
      %GC     time      71.2%  (71.5% elapsed)
    
      Alloc rate    1,689,230,189 bytes per MUT second
    
      Productivity  28.7% of total user, 28.7% of total elapsed
    

    对于 sumwith2

    ./heap +RTS -sstderr -K500M           
    500000500000
         256,057,488 bytes allocated in the heap
              65,256 bytes copied during GC
              30,240 bytes maximum residency (1 sample(s))
              21,440 bytes maximum slop
                   1 MB total memory in use (0 MB lost due to fragmentation)
    
                                        Tot time (elapsed)  Avg pause  Max pause
      Gen  0       488 colls,     0 par    0.00s    0.00s     0.0000s    0.0000s
      Gen  1         1 colls,     0 par    0.00s    0.00s     0.0003s    0.0003s
    
      INIT    time    0.00s  (  0.00s elapsed)
      MUT     time    0.14s  (  0.14s elapsed)
      GC      time    0.00s  (  0.00s elapsed)
      RP      time    0.00s  (  0.00s elapsed)
      PROF    time    0.00s  (  0.00s elapsed)
      EXIT    time    0.00s  (  0.00s elapsed)
      Total   time    0.14s  (  0.14s elapsed)
    
      %GC     time       1.8%  (1.8% elapsed)
    
      Alloc rate    1,798,840,354 bytes per MUT second
    
      Productivity  98.0% of total user, 99.3% of total elapsed
    

    您可以看到 GC 数量和使用的总内存有很大差异。有关更多信息,您可以参考 Don 指出的 RWH 章节。

    【讨论】:

    • 谢谢 Satvik。我将从 RWH 章节中挖掘更多内容。
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多