这是 2 个函数,fun1
需要 1 个参数,fun2
需要 4 个额外的无用参数。当我针对 x64 时,fun1
需要 4 秒但fun2
不到 1 秒。如果我以anycpu为目标,那么两者都需要不到1s。
我在这里问了一个类似的问题 ,如果目标是 x64,为什么 Seq.iter 比 for 循环快 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