From: "Radosław Bułat" Date: 2011-01-20T18:22:25+09:00 Subject: [ruby-core:34648] [Ruby 1.9-Bug#4298][Open] Duration of calling String#[] with the same index is strangely related to string length. --mimepart_4d37feb499f68_5400bacbd622143 Content-Type: text/plain Content-Transfer-Encoding: Quoted-printable Content-Disposition: inline Bug #4298: Duration of calling String#[] with the same index is strangely= related to string length. http://redmine.ruby-lang.org/issues/show/4298 Author: Rados=C5=82aw Bu=C5=82at Status: Open, Priority: Normal Category: core ruby -v: ruby 1.8.7 (2009-12-24 patchlevel 248) [i686-darwin10.0.0], MBAR= I 0x6770, Ruby Enterprise Edition 2010.01 The longer string is the slower is String#[] (for the same index > 0). Se= e the example benchmark below:

# encoding: utf-8

require 'benchmark'

s1 =3D "=C4=85=C4=87=C4=99=C5=82=C5=84=C3=B3=C5=9B=C5=BC=C5=BA!0123456789=
"

puts Benchmark.measure {
  1000.times { s1[1] }
}

s2 =3D "=C4=85=C4=87=C4=99=C5=82=C5=84=C3=B3=C5=9B=C5=BC=C5=BA!0123456789=
" * 100_000

puts Benchmark.measure {
  1000.times { s2[1] }
}
Running above code:
$ ruby-trunk x.rb =

  0.010000   0.000000   0.010000 (  0.000341)
  1.780000   0.020000   1.800000 (  1.998366)
---------------------------------------- http://redmine.ruby-lang.org --mimepart_4d37feb499f68_5400bacbd622143 Content-Type: text/x-ruby-script; name=benchmark.rb Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename=benchmark.rb IyBlbmNvZGluZzogdXRmLTgKCnJlcXVpcmUgJ2JlbmNobWFyaycKCnMxID0g IsSFxIfEmcWCxYTDs8WbxbzFuiEwMTIzNDU2Nzg5IgoKcHV0cyBCZW5jaG1h cmsubWVhc3VyZSB7CiAgMTAwMC50aW1lcyB7IHMxWzFdIH0KfQoKczIgPSAi xIXEh8SZxYLFhMOzxZvFvMW6ITAxMjM0NTY3ODkiICogMTAwXzAwMAoKcHV0 cyBCZW5jaG1hcmsubWVhc3VyZSB7CiAgMTAwMC50aW1lcyB7IHMyWzFdIH0K fQoK --mimepart_4d37feb499f68_5400bacbd622143--