10
考慮這個代碼在F#:爲什麼Seq.iter和Seq.map如此之慢?
let n = 10000000
let arr = Array.init n (fun _ -> 0)
let rec buildList n acc i = if i = n then acc else buildList n (0::acc) (i + 1)
let lst = buildList n [] 0
let doNothing _ =()
let incr x = x + 1
#time
arr |> Array.iter doNothing // this takes 14ms
arr |> Seq.iter doNothing // this takes 74ms
lst |> List.iter doNothing // this takes 19ms
lst |> Seq.iter doNothing // this takes 88ms
arr |> Array.map incr // this takes 33ms
arr |> Seq.map incr |> Seq.toArray // this takes 231ms!
lst |> List.map incr // this takes 753ms
lst |> Seq.map incr |> Seq.toList // this takes 2111ms!!!!
爲什麼Seq
模塊上的iter
和map
功能比Array
和List
模塊等同這麼多慢?
是有道理的,感謝您指出了這一點 – theburningmonk
雖然你可能是正確的關於實際原因。它並沒有真正在更深層次上回答這個問題。他們爲什麼選擇使用MoveNext。如通過linq庫完成的,您可以從類型檢查開始,在列表或數組的情況下選擇匹配版本,那麼對於大型序列的差異將會忽略不計 –