るりまでString#unpack1
へのリンクが各所から貼られていなかったので貼った。
Ruby 3でるりまのHTMLを生成できなくなっていたので直した。
Ruby 2.7からString.new
した時の最小capacityが63に減っていたので、それをるりまにも反映した。
未使用のインスタンス変数の警告が出ていたので直した
require 'base64' require 'securerandom' mini_data = SecureRandom.random_bytes(10) data = SecureRandom.random_bytes(1_000) big_data = SecureRandom.random_bytes(100_000) super_big_data = SecureRandom.random_bytes(10_000_000) len0 = (mini_data.bytesize * 1.4).to_i len1 = (data.bytesize * 1.4).to_i len2 = (big_data.bytesize * 1.4).to_i len3 = (super_big_data.bytesize * 1.4).to_i require 'benchmark' Benchmark.bmbm do |x| x.report('no cap - mini'){5000000.times{[mini_data].pack('m')}} x.report('cap - mini') {5000000.times{[mini_data].pack('m', buffer: String.new(capacity: len0))}} x.report('no cap - small'){1000000.times{[data].pack('m')}} x.report('cap - small') {1000000.times{[data].pack('m', buffer: String.new(capacity: len1))}} x.report('no cap - big'){10000.times{[big_data].pack('m')}} x.report('cap - big') {10000.times{[big_data].pack('m', buffer: String.new(capacity: len2))}} x.report('no cap - super big'){100.times{[super_big_data].pack('m')}} x.report('cap - super big') {100.times{[super_big_data].pack('m', buffer: String.new(capacity: len3))}} end
Rehearsal ------------------------------------------------------ no cap - mini 1.005779 0.002393 1.008172 ( 1.008875) cap - mini 2.093248 0.000258 2.093506 ( 2.095177) no cap - small 1.641927 0.000033 1.641960 ( 1.643708) cap - small 1.547132 0.000000 1.547132 ( 1.548625) no cap - big 1.152307 0.083217 1.235524 ( 1.236429) cap - big 1.047578 0.029939 1.077517 ( 1.078611) no cap - super big 1.039212 0.063253 1.102465 ( 1.103733) cap - super big 1.042010 0.016647 1.058657 ( 1.059911) -------------------------------------------- total: 10.764933sec user system total real no cap - mini 1.006987 0.000035 1.007022 ( 1.007696) cap - mini 2.108321 0.000000 2.108321 ( 2.110214) no cap - small 1.630748 0.000000 1.630748 ( 1.632402) cap - small 1.538120 0.000000 1.538120 ( 1.539674) no cap - big 1.164225 0.029981 1.194206 ( 1.195114) cap - big 1.080092 0.029938 1.110030 ( 1.111182) no cap - super big 1.060224 0.016603 1.076827 ( 1.078112) cap - super big 1.042683 0.033302 1.075985 ( 1.077166)
Base64.encode64
を速くできないかなと思って眺めていたのだけど、難しかった。