Skip to content

Instantly share code, notes, and snippets.

@kdwinter
Created August 17, 2010 17:05
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 kdwinter/530868 to your computer and use it in GitHub Desktop.
Save kdwinter/530868 to your computer and use it in GitHub Desktop.
Some interesting Ruby benchmarks
require 'benchmark'
Benchmark.bm 20 do |x|
x.report "define_method" do
25_000.times do
class MyClass
define_method :foo do
puts 'hi'
end
end
end
end
x.report "class_eval" do
25_000.times do
class MyClass
class_eval %{
def foo
puts 'hi'
end
}, __FILE__, __LINE__
end
end
end
end
## Ruby Enterprise Edition 1.8.7
# user system total real
# define_method 0.240000 0.010000 0.250000 ( 0.251318)
# class_eval 0.320000 0.000000 0.320000 ( 0.319751)
## Ruby 1.9.2rc2
# user system total real
# define_method 0.120000 0.010000 0.130000 ( 0.130996)
# class_eval 0.640000 0.000000 0.640000 ( 0.637815)
## JRuby 1.5.1
# user system total real
# define_method 0.756000 0.000000 0.756000 ( 0.699000)
# class_eval 2.866000 0.000000 2.866000 ( 2.866000)
require 'benchmark'
Benchmark.bm 20 do |x|
x.report 'for' do
25_000.times do
for i in 1..5
i += 1
end
end
end
x.report '#each' do
25_000.times do
(1..5).each do |i|
i += 1
end
end
end
end
## Ruby Enterprise Edition 1.8.7
# user system total real
# for 0.050000 0.000000 0.050000 ( 0.053650)
# #each 0.060000 0.000000 0.060000 ( 0.058752)
## Ruby 1.9.2rc2
# user system total real
# for 0.020000 0.000000 0.020000 ( 0.019428)
# #each 0.020000 0.000000 0.020000 ( 0.018324)
## JRuby 1.5.1
# user system total real
# for 0.376000 0.000000 0.376000 ( 0.330000)
# #each 0.093000 0.000000 0.093000 ( 0.093000)
require 'benchmark'
foo = 'foo'
bar = 'bar'
Benchmark.bm 20 do |x|
x.report 'string interpolation' do
100_000.times do
"#{foo} #{bar}"
end
end
x.report 'array join' do
100_000.times do
[foo, bar].join(' ')
end
end
end
## Ruby Enterprise Edition 1.8.7
# user system total real
# string interpolation 0.080000 0.000000 0.080000 ( 0.081084)
# array join 0.120000 0.000000 0.120000 ( 0.121376)
## Ruby 1.9.2rc2
# user system total real
# string interpolation 0.060000 0.000000 0.060000 ( 0.060890)
# array join 0.110000 0.000000 0.110000 ( 0.105644)
## JRuby 1.5.1
# user system total real
# string interpolation 0.287000 0.000000 0.287000 ( 0.240000)
# array join 0.140000 0.000000 0.140000 ( 0.140000)
require 'benchmark'
Benchmark.bm 20 do |x|
x.report 'symbol#to_proc' do
100_000.times do
%w(1 2 3).map(&:to_i)
end
end
x.report 'block' do
100_000.times do
%w(1 2 3).map {|i| i.to_i }
end
end
end
## Ruby Enterprise Edition 1.8.7
# user system total real
# symbol#to_proc 0.670000 0.000000 0.670000 ( 0.674756)
# block 0.260000 0.000000 0.260000 ( 0.256443)
## Ruby 1.9.2rc2
# user system total real
# symbol#to_proc 0.140000 0.000000 0.140000 ( 0.140275)
# block 0.150000 0.000000 0.150000 ( 0.154282)
## JRuby 1.5.1
# user system total real
# symbol#to_proc 1.182000 0.000000 1.182000 ( 1.136000)
# block 0.188000 0.000000 0.188000 ( 0.188000)
require 'benchmark'
name = 'foos'
Benchmark.bm 20 do |x|
x.report 'interpolation' do
100_000.times do
:"@#{name}"
end
end
x.report 'to_sym' do
100_000.times do
"@#{name}".to_sym
end
end
end
## Ruby Enterprise Edition 1.8.7
# user system total real
# interpolation 0.070000 0.000000 0.070000 ( 0.069094)
# #to_sym 0.080000 0.000000 0.080000 ( 0.077872)
## Ruby 1.9.2rc2
# user system total real
# interpolation 0.060000 0.000000 0.060000 ( 0.057004)
# #to_sym 0.050000 0.000000 0.050000 ( 0.056125)
## JRuby 1.5.1
# user system total real
# interpolation 0.521000 0.000000 0.521000 ( 0.468000)
# #to_sym 0.113000 0.000000 0.113000 ( 0.113000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment