Skip to content

Instantly share code, notes, and snippets.

@rainux
Created January 27, 2011 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rainux/799017 to your computer and use it in GitHub Desktop.
Save rainux/799017 to your computer and use it in GitHub Desktop.
Ruby string operation benchmark
rainux@immortal ~/devel/twi_meido
% YUKI.N> ruby d.rb
user system total real
0.770000 0.020000 0.790000 ( 0.802016)
0.730000 0.000000 0.730000 ( 0.740943)
0.760000 0.020000 0.780000 ( 0.793928)
user system total real
1.160000 0.010000 1.170000 ( 1.182798)
1.230000 0.030000 1.260000 ( 1.268377)
rainux@immortal ~/devel/twi_meido
% YUKI.N> ruby d.rb
user system total real
0.880000 0.000000 0.880000 ( 0.895912)
0.740000 0.010000 0.750000 ( 0.756707)
0.890000 0.020000 0.910000 ( 0.926466)
user system total real
1.560000 0.010000 1.570000 ( 1.582597)
1.840000 0.000000 1.840000 ( 1.837688)
# encoding: UTF-8
require 'benchmark'
n = 500000
Benchmark.bm do |x|
x.report do
n.times do
str = 'Ruby string operation benchmark'
str << 'a random string'
end
end
x.report do
n.times do
str = 'Ruby string operation benchmark'
str = str + 'a random string'
end
end
x.report do
n.times do
str = 'Ruby string operation benchmark'
str = "#{str}a random string"
end
end
end
a = 'AAAAAAA'
b = 'BBBBBBBBBBB'
c = 'CCCCCCCCCCCCCCCCCCCCCCCCC'
Benchmark.bm do |x|
x.report do
n.times do
str = "#{a} some string #{b} some string #{c}"
end
end
x.report do
n.times do
str = "%s some string %s some string %s" % [a, b, c]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment