Understanding the space versus time trade-off

The StructArrays.jl package provides a convenient mechanism to quickly turn an array of structs into a struct of arrays. We must recognize that the price we are paying is an additional copy of the data in memory. Hence, we are once again getting into the classic space versus time trade-off in computing.

Let's quickly look into our use case again. We can use the Base.summarysize function in the Julia REPL to see the memory footprint:

The Base.summarysize function returns the size of the object in bytes. We divided the number 1024 twice to arrive at the mega-byte unit. It is interesting to see that the struct of arrays, sa, is more memory efficient than the original array of structs, records. Nevertheless, we have two copies of data in memory.

Fortunately, we do have some options here if we want to conserve memory. First, we may just discard the original data in the records variable if we no longer need the data in that structure. We can even force the garbage collector to run, as follows:

Second, we can discard the sa variable when we are done with the computation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset