Skip to content

Instantly share code, notes, and snippets.

@andruby
Created October 5, 2011 16:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save andruby/1264985 to your computer and use it in GitHub Desktop.
Save andruby/1264985 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)
@andruby
Copy link
Author

andruby commented Oct 5, 2011

Conclusions:

  • Comparing symbols is a faster than comparing strings (2.0x~2.5x)
  • Integer comparison is even faster (2x in Rubinius, 1.4x in Ruby 1.9.2)
  • Using constants that point to integers carries no overhead in 1.9.2, ~25% in ree-1.8.7 and rbx

Between implementations:

  • Method calling: Rubinius is ~2x faster than Ruby 1.9.2, who in turn is ~2x faster than ree-1.8.7
  • Comparing symbols, integers and integer constants in Rubinius is ~as fast as calling a (boolean) method.
  • BUT: defining methods on the fly in Rubinius-2.0.0pre is 2x slower than regular Ruby
  • Ruby 1.9.2 has significantly faster comparisons than ree-1.8.7

NOTES:
Benches were run on a 2011 MacBook Pro 2.66 Ghz Core i7.

@andruby
Copy link
Author

andruby commented Oct 5, 2011

Added ruby-1.9.3-rc1
=> on the fly method definition is 86% faster than 1.9.2 !

@matt-42
Copy link

matt-42 commented Jun 15, 2012

Here is a C++ equivalent for string comparison using strcmp: https://gist.github.com/2936289
Why is ruby still 500+ times slower than C++ event after 17 years of active development?

@abique
Copy link

abique commented Jun 15, 2012

Good question!

@andruby
Copy link
Author

andruby commented Jun 15, 2012

An interpreted language will never be faster than a language that compiles ahead of time to native machine instructions.

That being said, the single design goal of Ruby is developer happiness, which I thoroughly enjoy. I would not want to build web applications in C++.

If I encounter a slow bottleneck after profiling my code that really needs to run faster, I use ruby-inline and just redefine the method in C or C++. Best of both worlds?

@abique
Copy link

abique commented Jun 15, 2012

But sooner or later you have to face performances, so are performances part of developer happiness ? :)

@andruby
Copy link
Author

andruby commented Jun 15, 2012

The best tool to use depends on the job. Generating html is not that cpu intensive, and in my opinion development speed is more important there. The storage layer is a different story, speed is much more important and that's why most databases are written in C.

@matt-42
Copy link

matt-42 commented Jun 15, 2012

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.

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