2

我遇到了存储问题。我有一个非常大的“神奇”数字,我想经常使用(将计算时间减少了惊人的 98%)。问题是这个数字需要将近 40 小时来计算,所以如果我能保存它并在合理的时间内重新加载它,那么节省的费用将是非常棒的。

这是说明问题的代表性代码。

num = 256...# Very large integer

num.size
=> 13584958492 # bytes

# To not get "string size to large error" when loading.
array_of_bytes = num # broken into sets of 100 MB

File.open("large_num", "a") do |line|
  array_of_bytes.each do |bytes|
    line.puts bytes
  end
end

# To load the number
num = 0

File.open("large_num", "r").each do |line|
  num = (num<<(8*10**8)) | line.to_i
end

# num is not equal to the input number

注意:我确实在制作数组之前通过位移 num 添加了必要数量的零,并在加载数字后将它们位移了。

所以,如果有人知道哪里出了问题以及如何解决它。或者,如果您知道更好的方法,我将不胜感激。

4

0 回答 0