RubyInline: March 2006 Archives

Best of both worlds

| | Comments (1)

Here is my very simple rule for optimizing:

Never ever code for speed until you need to code for speed!

Consider it an addendum to Knuth's "root of all evil" law. There are two corollaries to this law:

Always measure.

and

Only rewrite the slow part in C, not the whole damn thing.

Some of this came up recently because of a dialog last week on ruby-talk between Sascha Ebach and JEG2 about making FasterCSV even faster. James wants to keep the module as pure ruby (totally laudable goal) but Sascha thought it'd be neat to take advantage of RubyInline if it were available. I'm all for keeping it pure ruby, but I wanted to see how you'd do the dual implementation version in a way that still read clean. Here is my first scratch at it:

begin; require 'inline'; rescue LoadError; end

class MaybeFast
  def blah
    puts "slow version"
  end unless respond_to? :inline

  inline do |builder|
    builder.c 'void blah() { puts("fast version"); }'
  end if respond_to? :inline
end

MaybeFast.new.blah

About this Archive

This page is a archive of entries in the RubyInline category from March 2006.

RubyInline: December 2005 is the previous archive.

RubyInline: August 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.32-en