最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

julia - How to read a file into a vector? - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Jan 20 at 14:26 DarkBee 15.7k8 gold badges70 silver badges114 bronze badges asked Jan 20 at 14:17 user2138149user2138149 16.6k30 gold badges145 silver badges286 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论