Skip to content

Instantly share code, notes, and snippets.

@jasoares
Forked from andruby/comparison.rb
Created March 5, 2012 16:48
Show Gist options
  • Save jasoares/1979223 to your computer and use it in GitHub Desktop.
Save jasoares/1979223 to your computer and use it in GitHub Desktop.
ruby comparison benchmark
require 'benchmark'
def is_it_true?
true
end
CONSTANT = 1
BenchTimes = 1_000_000
Benchmark.bm(20) do |bm|
bm.report("String compare") do
BenchTimes.times { 'string' == 'string' }
end
bm.report("Symbol compare") do
BenchTimes.times { :symbol == :symbol }
end
bm.report("Integer compare") do
BenchTimes.times { 42 == 42 }
end
bm.report("Constant int compare") do
BenchTimes.times { CONSTANT == CONSTANT }
end
bm.report("method call") do
BenchTimes.times { is_it_true? }
end
obj = Object.new
bm.report("method definition") do
BenchTimes.times do
def obj.new_method
true
end
end
end
end
user system total real
String compare 0.253000 0.000000 0.253000 ( 0.213000)
Symbol compare 0.084000 0.000000 0.084000 ( 0.084000)
Integer compare 0.058000 0.000000 0.058000 ( 0.058000)
Constant int compare 0.077000 0.000000 0.077000 ( 0.077000)
method call 0.076000 0.000000 0.076000 ( 0.076000)
method definition 4.215000 0.000000 4.215000 ( 4.215000)
user system total real
String compare 0.207485 0.001812 0.209297 ( 0.261455)
Symbol compare 0.094083 0.000091 0.094174 ( 0.094181)
Integer compare 0.046870 0.000082 0.046952 ( 0.046964)
Constant int compare 0.059667 0.000159 0.059826 ( 0.059850)
method call 0.051584 0.000072 0.051656 ( 0.051685)
method definition 4.915829 0.010213 4.926042 ( 4.926215)
user system total real
String compare 0.330000 0.000000 0.330000 ( 0.333215)
Symbol compare 0.140000 0.000000 0.140000 ( 0.138651)
Integer compare 0.140000 0.000000 0.140000 ( 0.143397)
Constant int compare 0.180000 0.000000 0.180000 ( 0.177966)
method call 0.200000 0.000000 0.200000 ( 0.195159)
method definition 2.590000 0.000000 2.590000 ( 2.600206)
user system total real
String compare 0.290000 0.000000 0.290000 ( 0.282435)
Symbol compare 0.110000 0.000000 0.110000 ( 0.110358)
Integer compare 0.070000 0.000000 0.070000 ( 0.073060)
Constant int compare 0.070000 0.000000 0.070000 ( 0.073501)
method call 0.110000 0.000000 0.110000 ( 0.108959)
method definition 2.240000 0.030000 2.270000 ( 2.268168)
user system total real
String compare 0.270000 0.000000 0.270000 ( 0.269890)
Symbol compare 0.100000 0.000000 0.100000 ( 0.103971)
Integer compare 0.070000 0.000000 0.070000 ( 0.072439)
Constant int compare 0.080000 0.000000 0.080000 ( 0.071468)
method call 0.100000 0.000000 0.100000 ( 0.102600)
method definition 1.220000 0.110000 1.330000 ( 1.334018)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment