Small #JuliaLang tip: if you want to write several vectors to the same file, assuming that the dimensions coincide, you can do this just with

open("path", "w") do io
writedlm(io, [x y z])
end

**But! The command [x y z] generates a matrix, thus allocating a lot of memory**. Luckily, writedlm also accepts zip(x,y,z), which does the same and is orders of magnitude faster, since it does not allocate memory! #julia

I wonder if there is an equivalent way to do this also when x,y,z... are matrices with different amount of columns. Right now, I am mixing vectors with matrices and zip does only take the first vector of the matrix. But anyway, when it can be applied makes a great difference.

@vbuendiar Have you tried using the JLD package instead? It's quite convenient as it can export any object or set of objects as a dict and you can easily re-load them into their original variable names. docs.juliahub.com/JLD2/O1EyT/0

@johnabs Thanks! Probably would save up some time, since apparently constructing the dictionary allocates way less memory than the array construction [x y]. But for single vectors I believe the first option is still the best, as zip takes literally almost nothing (then one would have to benchmark if JLD is faster itself than writedlm, but that I haven't really checked).

Follow

@vbuendiar True, but as one who prefers maximum code reusability, I much prefer a "one and done" solution unless I'm working with TONS of data, but I'm just lazy 😂

@johnabs Hahahaha, everything has its own use. Small stupid tests that the JLD2 through FileIO are slower than writedlm for my use case.

But sure it is an amazing way to store serialized data, I never liked to work with HDF5 because it felt weird in Python, but I will definitely give a chance to this for other applications. Thx!!

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.