Created
October 5, 2011 16:52
-
-
Save andruby/1264985 to your computer and use it in GitHub Desktop.
ruby comparison benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
I also enjoy ruby and build web apps with it. There is a great community around. I don't expect ruby to be as fast as C, but I don't see how the fact that this language is interpreted justify such a difference in terms of performances.
I agree you don't always need something faster than ruby, but when I google about ruby slowness, ruby gc, ruby profiling, I can see a lot of people wasting time and money to find ways to improve ruby speed. Sometimes, I think that rewriting underthings from scratch in plain C/C++ will be a faster and funnier, use less resources and save some penguin :) ...
Ruby, at first look, can seem very simple and flexible, but It can also make your application more complex when you come at optimizations. Unfortunately, you realize it too late when you've already written thousands of lines so you're stuck with ruby, you don't want to throw everything away.
Ruby is easy to use, fast to learn, allows to builds prototypes fast. This makes its popularity. But as soon as you need speed, today compiled languages like C/C++ are the only solution.