InfoQ

News

Embedding C in Ruby for Performance

Posted by Peter Cooper on Jul 28, 2006

Community
Ruby
Topics
Research ,
Performance & Scalability
Tags
C

It's well accepted that Ruby is no performance champion. It's execution speed is slower than that of similar scripting languages like Perl and Python, and significantly slower than lower-level languages like C. Ruby was designed to be a quick language to code in and develop for, but not a quick language in terms of its performance.

On the Ruby newsgroup, comp.lang.ruby, Peter Hickman started a thread suggesting "For performance, write it in C." This approach has been traditionally popular for languages like Perl, and is now becoming common with Ruby. One of Hickman's demonstrations yielded over a 100 times speed improvement between some demonstration Perl code and a C implementation, so the benefits are obvious, and as a language with a compiler on nearly every platform, C makes a good choice for a fast, lower-level language with which to write high-performance code.

With Ruby, there are two options available for using C. The traditional way is to create an extension for Ruby in C that's compiled separately and included in, like a library. You can learn how to do this in How to Create a Ruby Extension in 5 Minutes. A new approach, however, is to use the RubyInline library, a library that allows you to write C alongside your Ruby code. For small routines this is significantly quicker than going to the efforts of writing a whole library in C (though not by much), and also 'cleaner' code-wise. Some Ruby source code demonstrating RubyInline is available.

Inspired by the newsgroup thread, Pat Eyler wrote " RubyInline, Making Making Things Faster Easier" with a demonstration and benchmark results of how he used RubyInline to significantly speed up the calculation of prime numbers, and then immediately followed it up with "RubyInline: Going a bit Further". Both articles provide a good look at the performance gains possible by using inline C.

Ruby extensions in 5 minutes by Alex Popescu Posted Jul 28, 2006 1:06 PM
OMG... I was hoping for satire. by Clinton Begin Posted Jul 28, 2006 4:04 PM
Re: ... by Alex Popescu Posted Jul 28, 2006 4:54 PM
Re: OMG... I was hoping for satire. by Pat Eyler Posted Jul 29, 2006 8:05 AM
inline is hardly new by Ryan Davis Posted Aug 1, 2006 2:46 PM
  1. Back to top

    Ruby extensions in 5 minutes

    Jul 28, 2006 1:06 PM by Alex Popescu

    I think the tutorial about creating ruby extensions in 5 minutes should be renamed somehow to: creating ruby extensions in 5 minutes (or a couple of days) :-] (for reference: additional requirements for a Ruby env and the other thread originated in this one: OneClick Installer: MinGW? or VC2005?).

    From this perspective RubyInline looks very interesting.

    ./alex
    --
    .w( the_mindstorm )p.

  2. Back to top

    OMG... I was hoping for satire.

    Jul 28, 2006 4:04 PM by Clinton Begin

    I was hoping this was a joke, but now all I can do is laugh anyway. This takes the cake.

    I give up.

  3. Back to top

    Re: ...

    Jul 28, 2006 4:54 PM by Alex Popescu

    Not wanting to start a flame, but I haven't caught the funny part :-[.

    ./alex
    --
    .w( the_mindstorm )p.

  4. Back to top

    Re: OMG... I was hoping for satire.

    Jul 29, 2006 8:05 AM by Pat Eyler

    I agree with Alex, I'm not sure why you think this is funny.

    Ruby provides decent profiling and benchmarking tools to help identify the bottlenecks in your program(s). If refactoring the Ruby for better performance doesn't help enough, the option to easily rewrite a method or two in C without having to throw away the rest of the Ruby program seems like a pretty clear win.

  5. Back to top

    inline is hardly new

    Aug 1, 2006 2:46 PM by Ryan Davis

    given that the project has been public since 2002, it is hardly new. I might also add that I was able to duplicate the 5 minute traditional effort (including editing/compiling/linking/loading/running) in about 30 seconds with much cleaner code. See my full response on the original article's page.

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.