gpt4 book ai didi

f# - 与 anycpu 相比,针对 x64 有时会导致非常糟糕的性能

转载 作者:行者123 更新时间:2023-12-04 04:35:34 26 4
gpt4 key购买 nike

这是 2 个函数,fun1接受 1 个参数,fun2需要 4 个额外的无用参数。当我针对 x64 时,fun1需要 4s 但 fun2耗时不到 1s。如果我针对 anycpu,那么两者都需要不到 1 秒。

我在这里问了一个类似的问题
why Seq.iter is 2x faster than for loop if target is for x64?

它在 .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

最佳答案

这不是一个完整的答案,它是问题的第一个诊断。

我可以使用相同的配置重现该行为。如果您在 Tools -> Options -> F# Tools -> F# Interactive 中打开 F# Interactive 64 位,您可以在那里观察到相同的行为。

不同于 the other question , x64 抖动不是问题。事实证明,项目属性中的“生成尾调用”选项会导致 test1 的显着放缓。与 test2 相比.如果您关闭该选项,则两种情况的速度相似。

另一方面,您可以使用 inline关键字 fun1所以不需要尾调用。无论fun2,两个例子在执行时间上再次具有可比性是否内联。

也就是说,添加 tail. 很奇怪。操作码到 fun1使它比(用同样的方法)慢得多 fun2 .您可以联系 F# 团队进行进一步调查。

关于f# - 与 anycpu 相比,针对 x64 有时会导致非常糟糕的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12755033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com