[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Bug? Fixnum#** is slower than Bignum#**

murphy

5/21/2005 10:13:00 PM

(original text by Wolfgang Nádasi-Donner)
The German Ruby forum (http://ru...) recognized a strange
behaviour of the Fixnum class.

In exponentation, Fixnum is much slower than Bignum, which lets us
assume that some kind of a problem is
in the implementation.

Example:

>>>>> Code (Ruby) >>>>>

require 'benchmark'

N = 10**6
B = 65891264863465298234965902602612457060348499377
F = 4628

Benchmark.bmbm(10) do |bm|
bm.report('Bignum') do N.times do
B ** 0.5
end end

bm.report('Fixnum') do N.times do
F ** 0.5
end end
end

>>>>> Result >>>>>
user system total real

Bignum 3.856000 0.000000 3.856000 ( 3.946000)
Fixnum 7.861000 0.020000 7.881000 ( 8.081000)

>>>>> End of Example >>>>>

Further investigations lead to the responsible C code:

>>>>> Code (C) >>>>>

// in numeric.c
static VALUE
fix_pow(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
long a, b;

b = FIX2LONG(y);
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
a = FIX2LONG(x);
if (b > 0) {
return rb_big_pow(rb_int2big(a), y);
}
return rb_float_new(pow((double)a, (double)b));
}
return rb_num_coerce_bin(x, y); // <-- this take a lot of time
}

// ...versus...

// in bignum.c
VALUE
rb_big_pow(x, y)
VALUE x, y;
{
double d;
long yy;

if (y == INT2FIX(0)) return INT2FIX(1);
switch (TYPE(y)) {
case T_FLOAT: // <-- this is fast
d = RFLOAT(y)->value;
break;

case T_BIGNUM:
...

case T_FIXNUM:
...

default:
return rb_num_coerce_bin(x, y);
}
return rb_float_new(pow(rb_big2dbl(x), d));
}

>>>>> End of Code >>>>>

If it's a feature, maybe someone can explain.
If it's a bug, maybe someone can fix ;)

German readers may be interested in the original thread:
http://www.rubyforen.de/topi...

2 Answers

AndyW

7/20/2011 1:35:00 PM

0

On 20/07/2011 14:20, Mentalguy2k8 wrote:

> It's basically a club for mostly professional men who do each other
> favours, enjoy plenty of social activities and raise a lot of money for
> charities. The difference between them and the "Rotary Clubs" is that
> (according to my old man who is a Mason allegedly) the Masons don't seek
> publicity for their fundraising efforts. And it costs quite a bit to be
> involved. And that's all I know because he won't tell me anything, and
> he never proposed me for membership for some reason :)

I'm pretty sure that telling what goes on in Rotary does not carry the
threat of being buried up to the neck at low tide and having your tongue
cut out.

You can also be an atheist Rotarian. IIRC you cannot be an atheist Mason
- that rules out my membership.


Andy

Mentalguy2k8

7/20/2011 3:23:00 PM

0


"AndyW" <Andy@nojunqmail.com> wrote in message
news:4e26da4a$0$22792$a8266bb1@newsreader.readnews.com...
> On 20/07/2011 14:20, Mentalguy2k8 wrote:
>
>> It's basically a club for mostly professional men who do each other
>> favours, enjoy plenty of social activities and raise a lot of money for
>> charities. The difference between them and the "Rotary Clubs" is that
>> (according to my old man who is a Mason allegedly) the Masons don't seek
>> publicity for their fundraising efforts. And it costs quite a bit to be
>> involved. And that's all I know because he won't tell me anything, and
>> he never proposed me for membership for some reason :)
>
> I'm pretty sure that telling what goes on in Rotary does not carry the
> threat of being buried up to the neck at low tide and having your tongue
> cut out.

I think my old man would have happily done that to Rotarians, he absolutely
detested them for some reason. Back when I was a kid, they used to pose for
photos in the paper next to a big cheque for the amount they'd raised, he
would rant about how Masons raised more money and didn't shout about it.

Of course looking back now, I can see where his "connections" came in handy
for him and it's pretty clear which of his friends were brothers. Never did
me any good, though.