.net - Difference between Array.zeroCreate and Array.init -
i wanted show colleague can allocate more 2gb of ram, made little test application.
let mega = 1 <<< 20 let konst x y = x let allocate1mb _ = array.init mega (konst 0uy) let memoryhog = array.parallel.init 8192 allocate1mb printfn "i'm done..." system.console.readkey() |> ignore
this works , see process happily hogging away @ system's memory. however, takes long - hence array.parallel.init
.
i noticed, same code not work, if write with
let allocate1mb _ = array.zerocreate mega
more precisely, no data allocated , takes no time.
so question; what difference in semantics between array.zerocreate , array.init?
i understand array.init
run konst 0uy
function each time, explain time difference. why array.zerocreate
not allocate memory?
from fsharp.core sources:
let inline init (count:int) (f: int -> 't) = if count < 0 invalidarg "count" inputmustbenonnegativestring let arr = (zerocreateunchecked count : 't array) = 0 count - 1 arr.[i] <- f arr let zerocreate count = if count < 0 invalidarg "count" (sr.getstring(sr.inputmustbenonnegative)) microsoft.fsharp.primitives.basics.array.zerocreateunchecked count let inline zerocreateunchecked (count:int) = (# "newarr !0" type ('t) count : 't array #)
as can see, both functions use zerocreateunchecked
under hood, compiled newarr
il supposed push new array onto stack.
the fact not consume memory in case might indicate kind of optimization responsible. think either jit or compiler removing operation since created array never used, , not happen in case of array.init
.
Comments
Post a Comment