これらは2つの関数であり、fun1
1つのパラメーターをfun2
取り、4つの余分な役に立たないパラメーターを取ります。x64をターゲットにした場合、fun1
4秒fun2
かかりますが、1秒未満かかります。anycpuをターゲットにした場合、どちらも1秒未満で完了します。
ターゲットがx64の場合、 Seq.iter がforループより2倍速いのはなぜですか?
.Net 4.5 Visual Studio 2012、F#3.0でコンパイルされ、Windows7x64で実行されます
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