InfoQ

News

Embedding C in Ruby for Performance

Posted by Peter Cooper on Jul 28, 2006 12:19 PM

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

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.