191

I like Ruby on Rails and I use it for all my web development projects. A few years ago there was a lot of talk about Rails being a memory hog and about how it didn't scale very well but these suggestions were put to bed by Gregg Pollack here.

Lately though, I've been hearing people saying that Ruby itself is slow.

  • Why is Ruby considered slow?

I do not find Ruby to be slow but then again, I'm just using it to make simple CRUD apps and company blogs. What sort of projects would I need to be doing before I find Ruby becoming slow? Or is this slowness just something that affects all programming languages?

  • What are your options as a Ruby programmer if you want to deal with this "slowness"?

  • Which version of Ruby would best suit an application like Stack Overflow where speed is critical and traffic is intense?

The questions are subjective, and I realise that architectural setup (EC2 vs standalone servers etc) makes a big difference but I'd like to hear what people think about Ruby being slow.

Finally, I can't find much news on Ruby 2.0 - I take it we're a good few years away from that then?

vladr
  • 65,483
  • 18
  • 129
  • 130
stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189

14 Answers14

189

Why is Ruby considered slow?

Because if you run typical benchmarks between Ruby and other languages, Ruby loses.

I do not find Ruby to be slow but then again, I'm just using it to make simple CRUD apps and company blogs. What sort of projects would I need to be doing before I find Ruby becoming slow? Or is this slowness just something that affects all programming languages?

Ruby probably wouldn't serve you well in writing a real-time digital signal processing application, or any kind of real-time control system. Ruby (with today's VMs) would probably choke on a resource-constrained computer such as smartphones.

Remember that a lot of the processing on your web applications is actually done by software developed in C. e.g. Apache, Thin, Nginx, SQLite, MySQL, PostgreSQL, many parsing libraries, RMagick, TCP/IP, etc are C programs used by Ruby. Ruby provides the glue and the business logic.

What are your options as a Ruby programmer if you want to deal with this "slowness"?

Switch to a faster language. But that carries a cost. It is a cost that may be worth it. But for most web applications, language choice is not a relevant factor because there is just not enough traffic justify using a faster language that costs much more to develop for.

Which version of Ruby would best suit an application like Stack Overflow where speed is critical and traffic is intense?

Other folks have answered this - JRuby, IronRuby, REE will make the Ruby part of your application run faster on platforms that can afford the VMs. And since it is often not Ruby that causes slowness, but your computer system architecture and application architecture, you can do stuff like database replication, multiple application servers, loadbalancing with reverse proxies, HTTP caching, memcache, Ajax, client-side caching, etc. None of this stuff is Ruby.

Finally, I can't find much news on Ruby 2.0 - I take it we're a good few years away from that then?

Most folks are waiting for Ruby 1.9.1. I myself am waiting for Rails 3.1 on Ruby 1.9.1 on JRuby.

Finally, please remember that a lot of developers choose Ruby because it makes programming a more joyful experience compared to other languages, and because Ruby with Rails enables skilled web developers to develop applications very quickly.

Ron DeVera
  • 14,394
  • 6
  • 41
  • 36
Jay Godse
  • 15,163
  • 16
  • 84
  • 131
  • 3
    after much consideration, I have decided that this is the best answer. Thanks, I like the analogy about the signal processing app. It's easier to see what people are talking about now after all these helpful answers. – stephenmurdoch Apr 27 '10 at 03:13
  • 1
    Yes, you were a couple of years away from ruby 2, [Ruby 2.0.0 Released on 24th Feb 2013](http://www.ruby-lang.org/en/news/2013/02/24/ruby-2-0-0-p0-is-released/) – Morgan May 16 '13 at 13:19
  • 3
    My experience from using Ruby 2.1 is that it is about 25% faster than the same app running in Ruby 2.0 – Matt Connolly May 09 '14 at 01:12
  • 17
    Languages aren't slow or fast, their implementations, interpreters and compilers are : ) – Zelphir Kaltstahl Dec 22 '15 at 11:31
  • 2
    @ZelphirKaltstahl I am late to this thread, but I would like to comment on your comment :) I agree interpreters and compilers play a major role on how a program performs, but the language is very important too. Dynamic languages like Ruby and Python are slower than static typed languages simply because they require lots of type checking at runtime. Monkey patching is cool, but it is expensive. – Wilson Freitas Sep 11 '20 at 19:09
122

First of all, slower with respect to what? C? Python? Let's get some numbers at the Computer Language Benchmarks Game:

Why is Ruby considered slow?

Depends on whom you ask. You could be told that:

  • 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)
  • Ruby method calls are slow (although, because of duck typing, they are arguably faster than in strongly typed interpreted languages)
  • Ruby (with the exception of JRuby) does not support true multithreading
  • etc.

But, then again, slow with respect to what? Ruby 1.9 is about as fast as Python and PHP (within a 3x performance factor) when compared to C (which can be up to 300x faster), so the above (with the exception of threading considerations, should your application heavily depend on this aspect) are largely academic.

What are your options as a Ruby programmer if you want to deal with this "slowness"?

Write for scalability and throw more hardware at it (e.g. memory)

Which version of Ruby would best suit an application like Stack Overflow where speed is critical and traffic is intense?

Well, REE (combined with Passenger) would be a very good candidate.

Community
  • 1
  • 1
vladr
  • 65,483
  • 18
  • 129
  • 130
  • 1
    Garbage collection itself is not necessarily slow, but the MRI's garbage collection is. If you need faster Ruby, you can also look at JRuby as well as REE. – Andreas Mar 27 '10 at 16:14
  • "Ruby 1.9 vs. C (gcc)" link is wrong –  Mar 27 '10 at 16:15
  • Thanks for the link to the computer language shootout Vlad. REE does interest me too, and since I have RVM installed, I imagine I'll have a shot at it soon. – stephenmurdoch Mar 27 '10 at 19:17
  • Why are you linking to pages that say OUT-OF-DATE! The benchmarks game has measurements made with up-to-date versions of those languages, made a few weeks ago. – igouy Mar 27 '10 at 22:06
  • 1
    @igouy, True, mid-2008 may have been extreme. I updated the links, but they will in turn become out-of-date in a few months. :) Either way, the hardware and some patchlevels may have been different, and a few additional tests were added, but the bigger picture of things did not change. – vladr Mar 28 '10 at 03:34
  • >> out-of-date in a few months << No. As new versions become available new measurements are made. – igouy Mar 28 '10 at 15:59
  • 12
    >> within the same order of magnitude << It's within the same order of magnitude if you live to 7 or live to 69. Is that difference insignificant? – igouy Mar 28 '10 at 16:21
  • 10
    @igouy, I don't know about you, but I am not a program to measure my lifespan in terms of execution speed. Where I DO care about execution speed, for example, is HTTP response rendering time. I know that I will not notice the difference between 7ms and 69ms rendering time (esp. when riding on top of 130ms network latency). I DO know that I will notice the difference between 7ms and 700ms, and I will CERTAINLY notice a difference between 7ms and 7s -- but no, not between 7ms and 69ms. – vladr Aug 07 '14 at 05:44
  • @vladr Thank you for explaining why "within the same order of magnitude" doesn't mean the difference is insignificant. – igouy Nov 16 '14 at 17:26
  • c seems to be more than 10 times faster – Pascalius Mar 08 '16 at 18:12
  • 3
    @vladr, what about the 70ms or 700ms? Can you notice that difference? – Paul Draper Oct 04 '16 at 04:52
60

Here's what the creator of Rails, David Heinemeier Hansson has to say:

Rails [Ruby] is for the vast majority of web applications Fast Enough. We got sites doing millions of dynamic page views per day. If you end up being with the Yahoo or Amazon front page, it's unlikely that an off-the-shelve framework in ANY language will do you much good. You'll probably have to roll your own. But sure, I'd like free CPU cycles too. I just happen to care much more about free developer cycles and am willing to trade the former for the latter.

i.e. throwing more hardware or machines at the problem is cheaper than hiring more developers and using a faster, but harder to maintain language. After all, few people write web applications in C.

Ruby 1.9 is a vast improvement over 1.8. The biggest problems with Ruby 1.8 are its interpreted nature (no bytecode, no compilation) and that method calls, one of the most common operations in Ruby, are particularly slow.

It doesn't help that pretty much everything is a method lookup in Ruby - adding two numbers, indexing an array. Where other languages expose hacks (Python's __add__ method, Perl's overload.pm) Ruby does pure OO in all cases, and this can hurt performance if the compiler/interpreter is not clever enough.

If I were writing a popular web application in Ruby, my focus would be on caching. Caching a page reduces the processing time for that page to zero, whatever language you are using. For web applications, database overhead and other I/O begins to matter a lot more than the speed of the language, so I would focus on optimising that.

rjh
  • 49,276
  • 4
  • 56
  • 63
  • 7
    "After all, few people write web applications in C." - Of course not, but many performance-critical web-sites moved e.g. to Scala. – Dario Mar 27 '10 at 16:05
  • 6
    I disagree with the 'throwing more hardware at it' is cheaper. It's hard to convince customers that they should pay more money for hosting every X months because their platform was designed for developers in mind. – Kevin Mar 27 '10 at 16:15
  • 9
    @Keven: surely development costs would be reduced? Otherwise what would be the point of using Ruby in the first place? – rjh Mar 27 '10 at 16:34
  • 4
    @Kevin That statement is a bit broad. If you'd need to set up a new server for every 10% traffic increase or so with some 100 visits per day, the customer would clearly have a right to complain. Realistically though, you usually need to have a lot more traffic to begin with and increase that by an order of magnitude, before the old hardware can't cope anymore. At that point the topic moves into "a good problem to have" territory and hardly anybody would complain about upgrading hardware. Also, no "customer" runs such a high traffic website without being aware of these kinds of things. – deceze Mar 28 '10 at 05:37
  • 5
    @Kevin - let's turn that around. "It's hard to convince customers they should wait 3 months for a new feature because their platform was designed with computers in mind." If that new feature would drastically increase revenue, it will pay for the extra hardware. Besides, picking a fast language from the outset is, for many applications, a premature optimization. The chances are that your bottleneck will be somewhere else: database reads, network latency, etc. – Nathan Long Apr 27 '12 at 12:46
  • @rjh throwing hardware is a quick and dirty solution to a problem. But it's a linear solution. If you need your app to scale you need an exponential solution to performance tuning. Caching, as you mention, is one such a solution. Better software architecture is another. Faster language (C,Java,.Net) is yet another (but not the best and not the easiest). – Nir Levy Jul 10 '13 at 07:19
34

Writing code is slow. Reading code is slow. Finding and fixing bugs is slow. Adding features and enhancements is slow. Anything that improves on the previous is a win. Very rarely is execution performance an issue.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • 31
    @GregS: execution performance is *always* an issue if it impacts usability. True, scanning an xml file for a string in one second or three doesn't matter from a pure numbers point of view, but a couple seconds difference can make a big difference in usability when you're talking about a user-facing application. – Bryan Oakley Mar 27 '10 at 16:02
  • 3
    @Bryan: Agreed, but I stand by my "very rarely" qualification. – President James K. Polk Mar 27 '10 at 16:10
  • 4
    Execution performance is always an issue, just how much of an issue is what we are talking about. How much interpreted code can you run on a mobile phone before users stop buying your app because it kills batteries? How long will a user wait for your page to load before closing the ad depriving you of ad revenue? Answer these kinds questions and you how much execution performance matters. – Sqeaky Mar 19 '14 at 17:54
  • 2
    @dev The tuning Ajax did was an algorithmic improvement. It saved a lot of resources for a small amount of effort by changing th asymptotics. Algorithmic improvements are very often worthwhile. – Demi Aug 19 '15 at 07:41
15

The answer is simple: people say ruby is slow because it is slow based on measured comparisons to other languages. Bear in mind, though, "slow" is relative. Often, ruby and other "slow" languages are plenty fast enough.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • yeah, that's what I was thinking, I mean, people say it's slow, but it's still darn quick for my requirements... – stephenmurdoch Mar 27 '10 at 19:19
  • 11
    >> it's still darn quick for my requirements << It's fast enough for everything that doesn't need to be fast :-) – igouy Mar 27 '10 at 22:09
  • i'am partially biases on this, maybe cox this is an outdated comment. now we have ruby 2.3, and from ruby 2.2 experience, i found that rails stack is heavy. if ones need a faster framework, try pidrano, its based on sinatra and they tried to do as close as possible to rails command, but much lighter. but they have not reached version 1.0 yet, still more to come, but from my test, it run nice and fast. I've work it with active record 5 and pidrano sprockets, borrowed from rails. With 200 concurrent connection, I'm getting 1.5s response without db query, with assets from sprockets – James Tan Jun 01 '16 at 04:09
5

Joel on Software - Ruby Performance Revisited quite well explains it. Might be outdated though...

I would recommend to just stick with it as you're used to Ruby on Rails,
if you ever meet a performance issue you might reconsider to use a different language and framework.

In that case I would really suggest C# with ASP.NET MVC 2, works very well for CRUD apps.

Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
  • Thanks for the link, I always like reading Joel's take on things. Interesting remark he makes about DHH's "bumper-sticker slogan"... – stephenmurdoch Mar 27 '10 at 19:18
  • Quote: "*This doesn’t apply to everyone, but when people say they have performance problems with Ruby or that they just need to be able to run code faster than the core Ruby language engine can run it, it doesn’t help to have Ruby advocates singing hymns about developer cycles vs. CPU cycles.*" Well said. – Marc.2377 Apr 22 '19 at 17:24
3

I would say Ruby is slow because not much effort has been spent in making the interpreter faster. Same applies to Python. Smalltalk is just as dynamic as Ruby or Python but performs better by a magnitude, see http://benchmarksgame.alioth.debian.org. Since Smalltalk was more or less replaced by Java and C# (that is at least 10 years ago) no more performance optimization work had been done for it and Smalltalk is still ways faster than Ruby and Python. The people at Xerox Parc and at OTI/IBM had the money to pay the people that work on making Smalltalk faster. What I don't understand is why Google doesn't spend the money for making Python faster as they are a big Python shop. Instead they spend money on development of languages like Go...

OlliP
  • 1,545
  • 11
  • 22
  • I think it is because Python has it's place already and is heavily used nowadays. If you need high performance there are many libraries you can use or weave and other stuff you can use. – Zelphir Kaltstahl Dec 22 '15 at 11:33
  • From what I read some effort has already yielded good results in Ruby 2.5. – Marc.2377 Apr 22 '19 at 17:26
2

Obviously, talking about speed Ruby loses. Even though benchmark tests suggest that Ruby is not so much slower than PHP. But in return, you are getting easy-to-maintain DRY code, the best out of all frameworks in various languages.

For a small project, you wont feel any slowness (I mean until like <50K users) given that no complex calculations are used in the code, just the mainstream stuff.

For a bigger project, paying for resources pays off and is cheaper than developer wages. In addition, writing code on RoR turns out to be much faster than any other.

In 2014 this magnitude of speed difference you're talking about is for most websites insignificant.

Rápli András
  • 3,869
  • 1
  • 35
  • 55
2

The way to deal with Ruby's performance in Web application is the same as with any other programming language:

ARCHITECTURE

This is easier to do in Rails than in most other Web Frameworks.

At the application level, by caching whatever is supposed to be cached and by managing the access to the DB in an intelligent way (since the bottleneck is usually on the "DB" access for most WEB apps).

Rails makes it very easy and natural to solve these problems. There are several abstractions for caching data, pages and fragments, and there are also very nice abstractions to deal with the SQL part in an optimised and reusable fashion (Active Record and AREL).

This is the reason why so many applications written in faster and not-so-expressive languages (like php) end up being slower than the Ruby counterparts. It's not so easy and elegant to tackle caching and querying with these languages than it is with Ruby.

At the infrastructure level it is reasonable to think of load balancing and all that stuff that I do not happen to know a lot about. I'd outsource that problem by hiring some platform as service provider, like Heroku or Engine Yard. Anyway. Deploying rails with load balancing is probably not very hard to do.

Pedro Rolo
  • 28,273
  • 12
  • 60
  • 94
2

Ruby is slower than C++ at a number of easily measurable tasks (e.g., doing code that is heavily dependent on floating point). This is not very surprising, but enough justification for some people to say that “Ruby is Slow” without qualification. They don't count the fact that it is far easier and safer to write Ruby code than C++.

The best fix is to use targeted modules written in another language (e.g., C, C++, Fortran) in your Ruby code. Those can do the heavy lifting and your scripts can focus on higher level coordination issues.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Comparisons are often made with Java, C#, Python, maybe Perl rather than C++. – rjh Mar 27 '10 at 16:00
  • 5
    Of course. But I can assure you (as a developer of Tcl) that people will *always* compare you with other languages unfairly. The fix is to use those other languages for components that you stitch together; doing it all in one language is a bit like using a single tool for all tasks. If all you have is a hammer, everything looks like a thumb. – Donal Fellows Mar 27 '10 at 16:03
  • nice idea about using foreign language modules when they are needed – stephenmurdoch Mar 27 '10 at 19:08
  • >> to say that “Ruby is Slow” without qualification << A couple of years ago they might have gone on to show Ruby programs that were slower than Tcl programs :-) – igouy Mar 27 '10 at 22:31
  • 1
    You know what they say about Lies, Damned Lies and Benchmarks. ;-) – Donal Fellows Mar 27 '10 at 23:44
  • "After all, facts are facts, and although we may quote one to another with a chuckle the words of the Wise Statesman, 'Lies--damned lies--and statistics,' still there are some easy figures the simplest must understand, and the astutest cannot wriggle out of." Leonard Henry Courtney, 1895 – igouy Apr 03 '10 at 03:32
2

First of all, do you care about what others say about the language you like? When it does the job it has to do, you're fine.

OO isn't the fastest way to execute code, but it does help in creating the code. Smart code is always faster than dumb code and useless loops. I'm an DBA and see a lot of these useless loops, drop them, use better code and queries and application is faster, much faster. Do you care about the last microsecond? You might have languages optimized for speed, others just do the job they have to do and can be maintained by many different programmers.

It's all just a choice.

Frank Heikens
  • 117,544
  • 24
  • 142
  • 135
1

People say that Ruby is slow because they compare Ruby programs to programs written in other languages. Maybe the programs you write don't need to be faster. Maybe for the programs you write Ruby isn't the bottleneck that's slowing things down.

Ruby 2.1 compared to Javascript V8

Ruby 2.1 compared to ordinary Lua

Ruby 2.1 compared to Python 3

igouy
  • 2,547
  • 17
  • 16
0

Performance is almost always about good design and optimized database interactions. Ruby does what most web sites need quite fast, especially more recent versions; and the speed of development and ease of maintenance provides a large payoff in costs and in keeping customers happy. I find JAVA to have slow execution performance for some tasks, and given the difficulty of developing in JAVA, many developers create slow applications regardless of the theoretical speed capability as demonstrated in benchmarks (benchmarks are generally contrived to show a specific and narrow capability). When I need intensive processing that isn't well suited to my database's capabilities, I choose C or Objective-C or some other truly high performance compiled language for those tasks depending on the platform. If I need to create a databased web application, I use RoR or sometimes C# ASP.NET depending on other requirements; because all platforms have strengths and weaknesses. Execution speed of the things your application does is important, but after all, if execution performance of one narrow aspect of a language is all that counts; then I might still be using Assembler language for everything.

-5

Ruby performs well for developer productivity. Ruby by nature forces test driven development because of the lack of types. Ruby performs well when used as a high level wrapper for C libraries. Ruby also performs well during long running processes when it is JIT-compiled to machine code via JVM or Rbx VM. Ruby does not perform well when it is required to crunch numbers in a short time with pure ruby code.

nurettin
  • 11,090
  • 5
  • 65
  • 85