Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Python faster than Ruby?

They seem to share a lot of the same characteristics but as far as I can tell, Python 2.5 is faster than 1.8.7 by a lot.

Is there a deeper underlying reason behind this?

like image 731
cduruk Avatar asked Jul 15 '10 04:07

cduruk


People also ask

What's faster Python or Ruby?

There is a perception that Python is faster than Ruby, and this has often led teams to prefer it over Ruby for web development. The Ruby community is painfully aware of this, and Ruby has gotten way faster over the years. Now, in benchmarks, Ruby performs just about as well as Python, if not better.

Why is Python used more than Ruby?

Python is generally better for educational use or for people who want to build quick programs rather than work as developers, while Ruby is better for commercial web applications. There are more specific differences when comparing Ruby versus Python, and they have in common that there are many ways to learn both.

Why is Ruby considered slow?

Ruby is an interpreted language and interpreted languages will tend to be slower than compiled ones. Ruby uses garbage collection (though C#, which also uses garbage collection, comes out two orders of magnitude ahead of Ruby, Python, PHP etc. in the more algorithmic, less memory-allocation-intensive benchmarks above)

Why Python is the fastest growing programming language?

Python supports a wide variety of third party tools which makes it much easier to to use and motivates the users to continue with. Python has a very simple and elegant syntax. It's much easier to read and write Python programs compared to other languages like: C++, Java, C#.


2 Answers

Nothing deep, I am pretty sure -- it's strictly a matter of implementation choices and maturity. Python was quite a bit slower in many aspects not so long ago, after all! Consider for example:

$ py24 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 10.8 usec per loop
$ py25 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 9.83 usec per loop
$ py26 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 8.12 usec per loop
$ py27 -mtimeit '[i+i for i in xrange(55)]'
100000 loops, best of 3: 6.35 usec per loop

Yep, all on the same machine (Macbook Pro, 2.4 GHz Intel Core 2 Duo, OSX 10.5), all "official" Mac releases from python.org (latest one of each x in the 2.x series). I have no 2.3 around to check, but I'd expect it to be a wee bit slower than 2.4.

This is just the kinds of speed-up that a lot of loving, painstaking work can achieve among successive releases of pretty much the same underlying architecture. Not as flashy as adding feechurz, but often vastly more useful in the real world!-)

I'm pretty sure, therefore, that Ruby can also stabilize on a sound, performance-robust underlying architecture, then start getting a steady stream of under-the-hood performance tweaks over the years to get (e.g.) the 40% or so further improvement we observe here has been happening in (at least some parts of) Python in the last few years.

like image 53
Alex Martelli Avatar answered Oct 04 '22 04:10

Alex Martelli


One reason is Python's being compiled into bytecode which is then executed by a highly optimized VM. AFAIK Ruby doesn't work this way in 1.8 and earlier - but interprets the trees on the fly.

Think of it this way:

Python:

  1. Parse code into ASTs
  2. Convert ASTs into bytecode
  3. Run bytecode on a VM

Ruby (prior to 1.9):

  1. Parse code into ASTs
  2. Interpret the ASTs directly by recursive traversal

Without getting too much into detail, step 2 in the old Ruby has a lot of repetitions because it has to "understand" the ASTs each time it sees them (which, in an inner loop is a lot). Python "understands" the ASTs only once, and then the VM runs the bytecode as fast as it can (which isn't different in principle from the way the Java and .NET VMs work).

Ruby 1.9 moved to YARV, which is also a VM-based approach. Ruby 1.9 is faster than 1.8. Here's a quote from the creator of YARV, Koichi Sasada:

At first, YARV is simple stack machine which run pseudo sequential instructions. Old interpreter (matzruby) traverses abstract syntax tree (AST) naively. Obviously it's slow. YARV compile that AST to YARV bytecode and run it.

An interesting point to note is that the Python VM is also stack based, just like YARV.

like image 20
Eli Bendersky Avatar answered Oct 04 '22 02:10

Eli Bendersky