From: Lyle Johnson Date: 2002-02-05T22:40:25+09:00 Subject: Re: Ruby and Swig?? > are there examples in the RAA which are using swig?? FXRuby (http://fxruby.sourceforge.net) uses SWIG extensively to generate its extension code. I suspect there are some other modules in the RAA using SWIG but don't know for sure. > I wonder when it is a good idea to use swig and when to > write the extensions by hand ? > are there performance differences ?? For small extensions (where the definition of "small" is up to you ;) using SWIG is probably overkill. Ruby's API for writing extensions in C/C++ is so easy to use that this is much easier than it would be for most other scripting languages. But for larger libraries, and especially large C++ libraries, you might want to take a look at SWIG. The only disadvantage is that the SWIG-generated source code tends to be larger than hand-written code (although this is improving). I don't think you'll see any measurable performance differences between SWIG-generated extension code and hand-written extension code, because most of the CPU time should be devoted to doing whatever "real work" it is that the extension does. As an example, pretend you need to generate wrapper code for some kind of scientific computing library written in C. The wrapped version of one of these function calls will look something like this: VALUE wrap_TimeConsumingComputation(VALUE self, VALUE arg1, VALUE arg2) { double x, y, result; x = NUM2DBL(arg1); y = NUM2DBL(arg2); result = TimeConsumingComputation(x, y); return rb_float_new(result); } Now it is true that SWIG-generated code *may* be slightly more verbose than this, perhaps introducing additional local variables to complete the type-conversions; but the calls to NUM2DBL() and rb_float_new() will presumably take *much* less time to complete than the call to TimeConsumingComputation(). Hope this helps, Lyle