Notes
Your code looks good, uses the correct Ruby methods and the correct syntax, but it is still:
- creates a huge array of integers
Alternative
, :
def get_binary_chunks(string, size)
Array.new(((string.length + size - 1) / size)) { |i| string.byteslice(i * size, size) }
end
(string.length + size - 1) / size) , , size.
500kB pdf 12345 Fruity :
Running each test 16 times. Test will take about 28 seconds.
_eric_duminil is faster than _b_seven by 380x ยฑ 100.0
get_binary_chunks 6 , StringIO#each(n).
, ( UTF8 'รค'), slice byteslice:
def get_binary_chunks(string, size)
Array.new(((string.length + size - 1) / size)) { |i| string.slice(i * size, size) }
end
( 500x ).
Unicode, size , size .
, , :
def send_binary_chunks(socket, string, size)
((string.length + size - 1) / size).times do |i|
socket.write string.slice(i * size, size)
end
end