ruby fast reading from std
问题 What is the fastest way to read from STDIN a number of 1000000 characters (integers), and split it into an array of one character integers (not strings) ? 123456 > [1,2,3,4,5,6] 回答1: This should be reasonably fast: a = [] STDIN.each_char do |c| a << c.to_i end although some rough benchmarking shows this hackish version is considerably faster: a = STDIN.bytes.map { |c| c-48 } 回答2: The quickest method I have found so far is as follows :- gets.unpack("c*").map { |c| c-48} Here are some results