Kurt StephensNerd Up! | ||
|
Kurt on Wed, 2007-03-28 22:23.
Ruby This RubyInline hack saved 15% execution time in a large Rails application:
require 'inline'
class Fixnum
inline do | builder |
builder.c_raw '
static
VALUE
gcd(int argc, VALUE *argv, VALUE self) {
if ( argc != 1 ) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)",
argc, 1);
}
/* Handle Fixnum#gcd(Fixnum) case directly. */
if ( FIXNUM_P(argv[0]) ) {
/* fprintf(stderr, "Using Fixnum#gcd(Fixnum)\n"); */
long a = FIX2LONG(self);
long b = FIX2LONG(argv[0]);
long min = a < 0 ? - a : a;
long max = b < 0 ? - b : b;
while ( min > 0 ) {
int tmp = min;
min = max % min;
max = tmp;
}
return LONG2FIX(max);
} else {
/* fprintf(stderr, "Using super#gcd\n"); */
return rb_call_super(1, argv);
}
}
'
end
end
Update: Sorry for the late reply. If the code above does not work via cut-and-paste, download it from here. This will be released soon as a gem dynamic library called speedfreaks, Thanks for the feedback! Reply |
||
Recent comments
6 days 11 hours ago
3 weeks 2 days ago
3 weeks 2 days ago
6 weeks 3 days ago
6 weeks 3 days ago
8 weeks 2 days ago
8 weeks 6 days ago
8 weeks 6 days ago
8 weeks 6 days ago
27 weeks 1 day ago