Project

General

Profile

Actions

Bug #4298

closed

Duration of calling String#[] with the same index is strangely related to string length.

Added by radarek (Radosław Bułat) over 13 years ago. Updated about 13 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 1.8.7 (2009-12-24 patchlevel 248) [i686-darwin10.0.0], MBARI 0x6770, Ruby Enterprise Edition 2010.01
Backport:
[ruby-core:34648]

Description

=begin
The longer string is the slower is String#[] (for the same index > 0). See the example benchmark below:


 # encoding: utf-8
 
 require 'benchmark'
 
 s1 = "ąćęłńóśżź!0123456789"
 
 puts Benchmark.measure {
   1000.times { s1[1] }
 }
 
 s2 = "ąćęłńóśżź!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)
 

=end


Files

benchmark.rb (228 Bytes) benchmark.rb Benchmark showing described problem radarek (Radosław Bułat), 01/20/2011 06:21 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0