Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News Embedding C in Ruby for Performance

Embedding C in Ruby for Performance


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.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Ruby extensions in 5 minutes

    by Alex Popescu,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

    .w( the_mindstorm )p.

  • OMG... I was hoping for satire.

    by Clinton Begin,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

    I give up.

  • Re: ...

    by Alex Popescu,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

    .w( the_mindstorm )p.

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

    by Pat Eyler,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

  • inline is hardly new

    by Ryan Davis,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.