声明:这是微基准,如果您对该主题感到不满意,请不要评论诸如“过早优化是邪恶的”之类的引用。
示例是针对 x64 的版本、.Net4.5 Visual Studio 2012 F# 3.0 并在 windows 7 x64 中运行
在分析之后,我缩小了我的一个应用程序的瓶颈,所以我想提出这个问题:
观察
for in
如果循环或内没有循环Seq.iter
,则很明显它们的速度相似。(更新 2 与更新 4)
for in
如果循环或内部有一个循环Seq.iter
,它似乎Seq.iter
是 2 倍快for in
。(更新与更新3)奇怪吗?(如果在 fsi 中运行,它们将是相似的)
如果是针对anycpu,运行在x64,时间上没有区别。所以问题变成了:如果目标是 x64,Seq.iter (update3) 将提高 2 倍速度
用的时间:
update: 00:00:11.4250483 // 2x as much as update3, why?
updatae2: 00:00:01.4447233
updatae3: 00:00:06.0863791
updatae4: 00:00:01.4939535
源代码:
open System.Diagnostics
open System
[<EntryPoint>]
let main argv =
let pool = seq {1 .. 1000000}
let ret = Array.zeroCreate 100
let update pool =
for x in pool do
for y in 1 .. 200 do
ret.[2] <- x + y
let update2 pool =
for x in pool do
//for y in 1 .. 100 do
ret.[2] <- x
let update3 pool =
pool
|> Seq.iter (fun x ->
for y in 1 .. 200 do
ret.[2] <- x + y)
let update4 pool =
pool
|> Seq.iter (fun x ->
//for y in 1 .. 100 do
ret.[2] <- x)
let test n =
let run = match n with
| 1 -> update
| 2 -> update2
| 3 -> update3
| 4 -> update4
for i in 1 .. 50 do
run pool
let sw = new Stopwatch()
sw.Start()
test(1)
sw.Stop()
Console.WriteLine(sw.Elapsed);
sw.Restart()
test(2)
sw.Stop()
Console.WriteLine(sw.Elapsed)
sw.Restart()
test(3)
sw.Stop()
Console.WriteLine(sw.Elapsed)
sw.Restart()
test(4)
sw.Stop()
Console.WriteLine(sw.Elapsed)
0 // return an integer exit code