why is Seq.iter and Seq.map so much slower?

后端 未结 1 1378
小蘑菇
小蘑菇 2021-01-11 15:22

Consider this code in F#:

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条回答
  • 2021-01-11 16:09

    Once you call in to Seq you lose the type information - moving to the next element in the list requires a call to IEnumerator.MoveNext. Compare to for Array you just increment an index and for List you can just dereference a pointer. Essentially, you are getting an extra function call for each element in the list.

    The conversions back to List and Array also slow the code down for similar reasons

    0 讨论(0)
提交回复
热议问题