Skip to content

Instantly share code, notes, and snippets.

@x77686d
Created March 7, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x77686d/be8027586f24428d0cee to your computer and use it in GitHub Desktop.
Save x77686d/be8027586f24428d0cee to your computer and use it in GitHub Desktop.
Demonstrates exponential slowdown with string[n] = "x" as n grows.
=begin
It seems that Ruby 2.2.4, string[n] = "x" is not O(1). It was suggested that
this was due to UTF-8 encoding, but I see the same performance with US-ASCII.
Any ideas?
$ ruby --version
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
$ time ruby -E US-ASCII:US-ASCII storeseq2.rb 1000 1000
["Encodings: ", #<Encoding:US-ASCII>, #<Encoding:US-ASCII>]
real 0m0.636s
user 0m0.584s
sys 0m0.016s
$ time ruby -E US-ASCII:US-ASCII storeseq2.rb 10_000 100
["Encodings: ", #<Encoding:US-ASCII>, #<Encoding:US-ASCII>]
real 0m1.770s
user 0m1.712s
sys 0m0.052s
$ time ruby -E US-ASCII:US-ASCII storeseq2.rb 100_000 10
["Encodings: ", #<Encoding:US-ASCII>, #<Encoding:US-ASCII>]
real 0m12.671s
user 0m12.649s
sys 0m0.012s
$ time ruby -E US-ASCII:US-ASCII storeseq2.rb 1_000_000 1
["Encodings: ", #<Encoding:US-ASCII>, #<Encoding:US-ASCII>]
real 2m4.756s
user 2m3.976s
sys 0m0.660s
$ rvm 1.9.3
$ ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
$ time ruby -E US-ASCII:US-ASCII storeseq2.rb 1_000_000 1
["Encodings: ", #<Encoding:US-ASCII>, #<Encoding:US-ASCII>]
real 0m0.493s
user 0m0.480s
sys 0m0.012s
=end
if ARGV.size != 2
puts "Usage: STRING-LENGTH REPS"
exit
end
p ["Encodings: ", Encoding.default_external, Encoding.default_internal]
s = " " * ARGV[0].to_i
ARGV[1].to_i.times do
ARGV[0].to_i.times do
|i|
s[i] = "x"
end
end
@x77686d
Copy link
Author

x77686d commented Mar 7, 2016

Update: It looks like this is due to default UTF-8 encoding for string literals.

Adding '# encoding: US-ASCII' produces linear time behavior, without needing -E argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment