Kurt StephensNerd Up! | ||
Ruby: Date / Rational / Fixnum#gcd hack increased app performance by 15%Kurt on Wed, 2007-03-28 22:23.
UPDATE: 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! submitSubmitted by Roger (not verified) on Wed, 2007-05-09 11:58.
you might consider submitting this to the Ruby core people. Why not?
»
I think your code got hosedSubmitted by RR (not verified) on Tue, 2007-07-10 13:51.
I think the paste hosed your code, since FIX2LONG and LONG2FIX require params, thus the above code section does not compile. Looks like the correct code would be, “long a= FIX2LONG’(self)’;” and also “return LONG2FIX’(max)’;” —minus the single ticks (had to put those in for the comment to correctly render). Copy-paste compilation of the original code also errors because of the invalid quote characters. Thanks for sharing the trick! »
When’s speedfreaksSubmitted by Anonymous (not verified) on Tue, 2007-10-23 13:26.
When’s speedfreaks coming? I’ve been looking forward to it for a while now. My application is using up a lot of CPU time on my host and I’m desperate for ways to speed it up. »
Ruby 1.9 and a Gem soon?Submitted by Kurt on Fri, 2007-12-14 02:51.
Fixnum#gcd patch has been submitted for Ruby 1.8 and 1.9 core. There are some hacks that I’m testing at work. We’ll probably roll them together soon. — Kurt »
try againSubmitted by roger degree search (not verified) on Tue, 2008-07-01 19:07.
Maybe creating a ticket on http://redmine.ruby-lang.org/issues/show/174 would cause more publicity :) »
gemSubmitted by roger degree search (not verified) on Fri, 2008-07-18 12:33.
Is this a drop in replacement for it? If so you could release a gem that integrates it, a la »
Rejected againSubmitted by roger faith (not verified) on Tue, 2008-10-07 11:52.
Appears that the patch was rejected as “already applied” for ruby 1.9—is that the case? »
already appliedSubmitted by Anonymous (not verified) on Mon, 2008-12-08 02:46.
I did some of my own tests and it appears that ruby 1.9 “already has” an optimized gcd. Do yours differ? »
|
||
Recent comments
1 week 1 day ago
3 weeks 4 days ago
3 weeks 6 days ago
3 weeks 6 days ago
3 weeks 6 days ago
3 weeks 6 days ago
6 weeks 2 days ago
6 weeks 2 days ago
8 weeks 5 days ago
8 weeks 6 days ago