This post originated from an RSS feed registered with Ruby Buzz
by Ryan Davis.
Original Post: Best of both worlds
Feed Title: Polishing Ruby
Feed URL: http://blog.zenspider.com/index.rdf
Feed Description: Musings on Ruby and the Ruby Community...
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'; rescueLoadError; end
class MaybeFast def blah puts"slow version" endunless respond_to? :inline
inline do |builder|
builder.c 'void blah() { puts("fast version"); }' endif respond_to? :inline end