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
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