The following code, which can be run in the Julia REPL, shows how to write the contents of a vector to disk.
v = rand(Int64, 1000000)
ofile = open("example_vector.bin", "w")
write(ofile, v)
close(ofile)
The reverse operation is presumably possible as well, however I cannot figure out the correct syntax for this.
ifile = open("example_vector.bin", "r")
v2 = Vector{Int64}(undef, 1000000)
read(ifile, v) # this is not valid, perhaps unsurprisingly
# I tried a few other things which I thought might work, but none did
read(ifile, Vector{Int64}, 1000000)
read(ifile, Vector{Int64, 1000000})
read(ifile, ::Type{Vector{Int64}}, 1000000)
I have already tried the following things:
- Searching the web for solutions
- Asking ChatGPT (unsurprisingly it made up a function call which did not exist)
- Reading the documentation for
read
using the?
mode in the REPL
What I want to avoid is having to make 1 million function calls to read
to read each element of the vector individually, which is likely to result in poor performance.
The following code, which can be run in the Julia REPL, shows how to write the contents of a vector to disk.
v = rand(Int64, 1000000)
ofile = open("example_vector.bin", "w")
write(ofile, v)
close(ofile)
The reverse operation is presumably possible as well, however I cannot figure out the correct syntax for this.
ifile = open("example_vector.bin", "r")
v2 = Vector{Int64}(undef, 1000000)
read(ifile, v) # this is not valid, perhaps unsurprisingly
# I tried a few other things which I thought might work, but none did
read(ifile, Vector{Int64}, 1000000)
read(ifile, Vector{Int64, 1000000})
read(ifile, ::Type{Vector{Int64}}, 1000000)
I have already tried the following things:
- Searching the web for solutions
- Asking ChatGPT (unsurprisingly it made up a function call which did not exist)
- Reading the documentation for
read
using the?
mode in the REPL
What I want to avoid is having to make 1 million function calls to read
to read each element of the vector individually, which is likely to result in poor performance.
2 Answers
Reset to default 2Whenever you are looking for a function that works in-place or mutates an argument, you are looking for functions that end with !
. In this case, you are looking for read!
, which does exactly what you want:
https://docs.julialang.org/en/v1/base/io-network/#Base.read!
read
has an overload that likely would help you:
read(s::IOStream, nb::Integer; all=true)
It reads at most n bytes from an input stream and stores them as Vector{UInt8}
, as a raw binary data. Afterwards, it would be your job to coerce this binary data to expected Vector{Int64}
. This solution likely to be more efficient compared to reading bytes one-by-one, though, it requires O(N)
linear scan for coercion and O(N)
additional memory.