为什么F#中的功能组成比管道慢得多60%?

时间:2016-09-06 12:54:29

标签: performance f# piping function-composition

不可否认,我不确定我是否正确地将苹果与苹果或苹果与梨进行比较。但我对这种差异的巨大性感到特别惊讶,如果有任何差异,那将会有更大的差异。

管道can often be expressed as function composition and vice versa,我认为编译器也知道这一点,所以我尝试了一点实验:

// simplified example of some SB helpers:
let inline bcreate() = new StringBuilder(64)
let inline bget (sb: StringBuilder) = sb.ToString()
let inline appendf fmt (sb: StringBuilder) = Printf.kbprintf (fun () -> sb) sb fmt
let inline appends (s: string) (sb: StringBuilder) = sb.Append s
let inline appendi (i: int) (sb: StringBuilder) = sb.Append i
let inline appendb (b: bool) (sb: StringBuilder) = sb.Append b

// test function for composition, putting some garbage data in SB
let compose a =            
    (appends "START" 
    >> appendb  true
    >> appendi 10
    >> appendi a
    >> appends "0x"
    >> appendi 65535
    >> appendi 10
    >> appends "test"
    >> appends "END") (bcreate())

// test function for piping, putting the same garbage data in SB
let pipe a =
    bcreate()
    |> appends "START" 
    |> appendb  true
    |> appendi 10
    |> appendi a
    |> appends "0x"
    |> appendi 65535
    |> appendi 10
    |> appends "test"
    |> appends "END"

在FSI中测试(启用64位,--optimize标志开启)给出:

> for i in 1 .. 500000 do compose 123 |> ignore;;
Real: 00:00:00.390, CPU: 00:00:00.390, GC gen0: 62, gen1: 1, gen2: 0
val it : unit = ()
> for i in 1 .. 500000 do pipe 123 |> ignore;;
Real: 00:00:00.249, CPU: 00:00:00.249, GC gen0: 27, gen1: 0, gen2: 0
val it : unit = ()

一个小的差异是可以理解的,但这是因素1.6(60%)的性能下降。

我实际上预计大部分工作都会在StringBuilder中发生,但显然构图的开销会产生相当大的影响。

我意识到在大多数实际情况下,这种差异可以忽略不计,但如果您正在编写大型格式化文本文件(如日志文件),则会产生影响。

我使用的是最新版本的F#。

2 个答案:

答案 0 :(得分:10)

我用FSI试了你的例子,发现没有明显的区别:

> #time
for i in 1 .. 500000 do compose 123 |> ignore

--> Timing now on

Real: 00:00:00.229, CPU: 00:00:00.234, GC gen0: 32, gen1: 32, gen2: 0
val it : unit = ()
> #time;;

--> Timing now off

> #time
for i in 1 .. 500000 do pipe 123 |> ignore;;;;

--> Timing now on

Real: 00:00:00.214, CPU: 00:00:00.218, GC gen0: 30, gen1: 30, gen2: 0
val it : unit = ()

BenchmarkDotNet中测量它(第一个表只是一个组合/管道运行,第二个表是500000次),我发现了类似的东西:

  Method | Platform |       Jit |      Median |     StdDev |    Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
-------- |--------- |---------- |------------ |----------- |--------- |------ |------ |------------------- |
 compose |      X64 |    RyuJit | 319.7963 ns |  5.0299 ns | 2,848.50 |     - |     - |             182.54 |
    pipe |      X64 |    RyuJit | 308.5887 ns | 11.3793 ns | 2,453.82 |     - |     - |             155.88 |
 compose |      X86 | LegacyJit | 428.0141 ns |  3.6112 ns | 1,970.00 |     - |     - |             126.85 |
    pipe |      X86 | LegacyJit | 416.3469 ns |  8.0869 ns | 1,886.00 |     - |     - |             121.86 |

  Method | Platform |       Jit |      Median |    StdDev |    Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
-------- |--------- |---------- |------------ |---------- |--------- |------ |------ |------------------- |
 compose |      X64 |    RyuJit | 160.8059 ms | 4.6699 ms | 3,514.75 |     - |     - |      56,224,980.75 |
    pipe |      X64 |    RyuJit | 163.1026 ms | 4.9829 ms | 3,120.00 |     - |     - |      50,025,686.21 |
 compose |      X86 | LegacyJit | 215.8562 ms | 4.2769 ms | 2,292.00 |     - |     - |      36,820,936.68 |
    pipe |      X86 | LegacyJit | 209.9219 ms | 2.5605 ms | 2,220.00 |     - |     - |      35,554,575.32 |

您测量的差异可能与GC有关。尝试在您的计时之前/之后强制收集GC。

那就是说,查看管道运营商的source code

let inline (|>) x f = f x

并与组合运算符进行比较:

let inline (>>) f g x = g(f x)

似乎清楚地表明组合运算符将创建lambda函数,这将导致更多的分配。这也可以在BenchmarkDotNet运行中看到。这也可能是您所看到的性能差异的原因。

答案 1 :(得分:7)

在没有任何关于F#内部的深入知识的情况下,我从生成的IL中可以看出,compose将产生lambdas(如果关闭优化,则会产生大量的lambdas),而在pipe所有将内联对append*的来电。

pipe函数生成IL:

Main.pipe:
IL_0000:  nop         
IL_0001:  ldc.i4.s    40 
IL_0003:  newobj      System.Text.StringBuilder..ctor
IL_0008:  ldstr       "START"
IL_000D:  callvirt    System.Text.StringBuilder.Append
IL_0012:  ldc.i4.1    
IL_0013:  callvirt    System.Text.StringBuilder.Append
IL_0018:  ldc.i4.s    0A 
IL_001A:  callvirt    System.Text.StringBuilder.Append
IL_001F:  ldarg.0     
IL_0020:  callvirt    System.Text.StringBuilder.Append
IL_0025:  ldstr       "0x"
IL_002A:  callvirt    System.Text.StringBuilder.Append
IL_002F:  ldc.i4      FF FF 00 00 
IL_0034:  callvirt    System.Text.StringBuilder.Append
IL_0039:  ldc.i4.s    0A 
IL_003B:  callvirt    System.Text.StringBuilder.Append
IL_0040:  ldstr       "test"
IL_0045:  callvirt    System.Text.StringBuilder.Append
IL_004A:  ldstr       "END"
IL_004F:  callvirt    System.Text.StringBuilder.Append
IL_0054:  ret

compose函数生成IL:

Main.compose:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  newobj      Main+compose@10..ctor
IL_0007:  stloc.1     
IL_0008:  ldloc.1     
IL_0009:  newobj      Main+compose@10-1..ctor
IL_000E:  stloc.0     
IL_000F:  ldc.i4.s    40 
IL_0011:  newobj      System.Text.StringBuilder..ctor
IL_0016:  stloc.2     
IL_0017:  ldloc.0     
IL_0018:  ldloc.2     
IL_0019:  callvirt    Microsoft.FSharp.Core.FSharpFunc<System.Text.StringBuilder,System.Text.StringBuilder>.Invoke
IL_001E:  ldstr       "END"
IL_0023:  callvirt    System.Text.StringBuilder.Append
IL_0028:  ret

compose@10.Invoke:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  ldfld       Main+compose@10.a
IL_0007:  ldarg.1     
IL_0008:  call        Main.f@1
IL_000D:  ldc.i4.s    0A 
IL_000F:  callvirt    System.Text.StringBuilder.Append
IL_0014:  ret         

compose@10..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        Microsoft.FSharp.Core.FSharpFunc<System.Text.StringBuilder,System.Text.StringBuilder>..ctor
IL_0006:  ldarg.0     
IL_0007:  ldarg.1     
IL_0008:  stfld       Main+compose@10.a
IL_000D:  ret         

compose@10-1.Invoke:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  ldfld       Main+compose@10-1.f
IL_0007:  ldarg.1     
IL_0008:  callvirt    Microsoft.FSharp.Core.FSharpFunc<System.Text.StringBuilder,System.Text.StringBuilder>.Invoke
IL_000D:  ldstr       "test"
IL_0012:  callvirt    System.Text.StringBuilder.Append
IL_0017:  ret         

compose@10-1..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        Microsoft.FSharp.Core.FSharpFunc<System.Text.StringBuilder,System.Text.StringBuilder>..ctor
IL_0006:  ldarg.0     
IL_0007:  ldarg.1     
IL_0008:  stfld       Main+compose@10-1.f
IL_000D:  ret