针对x64有时会导致非常糟糕的性能

col*_*ang 9 f#

这些是2个功能,fun1需要1个参数,fun2需要4个额外的无用参数.当我针对x64时,fun1需要4秒但fun2不到1 秒.如果我针对anycpu,那么两者都不到1秒.

我问这里有一个类似的问题 ,如果目标是x64,为什么Seq.iter比循环快2倍?

它是在.Net 4.5 Visual Studio 2012,F#3.0中编译,在Windows 7 x64中运行

open System
open System.Diagnostics

type Position =
    {
        a: int
        b: int
    }

[<EntryPoint>]
let main argv = 

    let fun1 (pos: Position[]) =  //<<<<<<<< here
        let functionB x y z = 4

        Array.fold2 (fun acc x y -> acc + int64 (functionB x x y)) 0L pos pos

    let fun2 (pos: Position[]) u v w x =  //<<<<<<<< here
        let functionB x y z = 4

        Array.fold2 (fun acc x y -> acc + int64 (functionB x x y)) 0L pos pos



    let s = {a=2;b=3}
    let pool = [|s;s;s|]

    let test1 n =
        let mutable x = 0L
        for i in 1 .. n do
            x <- fun1 pool

    let test2 n =
        let mutable x = 0L
        for i in 1 .. n do
            x <- fun2 pool 1 2 3 4

    let sw = new Stopwatch()
    sw.Start()
    test2 10000000
    sw.Stop()
    Console.WriteLine(sw.Elapsed)

    sw.Restart()
    test1 10000000
    sw.Stop()
    Console.WriteLine(sw.Elapsed)


    0 // return an integer exit code
Run Code Online (Sandbox Code Playgroud)

Gre*_*Ros 0

几乎可以肯定,这种差异是 JITer 的一个怪癖。它还解释了不一致的结果。这是此类微基准测试的常见问题。对这些方法执行一次或多次冗余执行,以便在幕后编译整个过程,并对最后一次进行计时。它们将是相同的。

由于这个怪癖,你可以获得比这更奇怪的结果。