From: Josh Cheek Date: 2012-03-25T02:55:45+09:00 Subject: Re: Ruby speed compared to C in a simple calculations. --f46d0444ee2d99cc3c04bc00d9ad Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Sat, Mar 24, 2012 at 11:45 AM, =D0=A0=D0=BE=D0=BC=D0=B0=D0=BD =D0=A2=D0= =BA=D0=B0=D0=BB=D0=B5=D0=BD=D0=BA=D0=BE wrote: > #works for 3.55 mins > P =3D 295075153 > o1 =3D 210205973 > o2 =3D 22795300 > (0 .. P).each do | i | > if i ^ (i ^ o1) =3D=3D o1 and ((i * 2 + 5) % P) ^ (((i ^ o1) * 3 + 7)= % P) > =3D=3D o2 > puts i > break > end > end > Code written in C gives me the solution in only 2 seconds. I suppose that > it's called not by ruby slowlyness, but my shitty coding abilities. Cuold > tou please tell me the weaknesse of the code? > It's not shitty coding (though I do find your code unreadable). It's because C is compiled into assembly, when you tell it to iterate that many times, it can put the number directly into a register and decrement the value. Ruby is much higher level than that. Numbers are actual objects that must be allocated, for example. So an example like this, C is going to dramatically outperform Ruby. Like anything, there are tradeoffs. For instance, by representing numbers with just binary values that fit into C's int type, you your program up to the limitations of that type (e.g. in Ruby you can have arbitrarily large numbers `$ruby -e 'p 2**2000'`). And, of course, the Ruby version uses a closure. ANSI C doesn't have closures, which prevents it from being able to support very powerful coding styles (i.e. many of the things Lisp is famous for). So, really its best to figure out what your domain is and find a language well suited to that. If your domain is hardcore number crunching, then Ruby is probably not the best choice (though I did solve a number of Project Euler problems with it). -Josh --f46d0444ee2d99cc3c04bc00d9ad Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Sat, Mar 24, 2012 at 11:45 AM, =D0=A0=D0= =BE=D0=BC=D0=B0=D0=BD =D0=A2=D0=BA=D0=B0=D0=BB=D0=B5=D0=BD=D0=BA=D0=BE <rain.roman@gmail.= com> wrote:
#works for 3.55 mins
P =3D 295075153
o1 =3D 210205= 973
o2 =3D 22795300
(0 .. P).each do | i |
= =C2=A0 =C2=A0 if i ^ (i ^ o1) =3D=3D o1 and ((i * 2 + 5) % P) ^ (((i ^ o1) = * 3 + 7) % P) =3D=3D o2
=C2=A0 =C2=A0 =C2=A0 =C2=A0 puts i
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 break
=C2=A0 =C2=A0 end
end
Code written = in C gives me the solution in only 2 seconds. I suppose that it's calle= d not by ruby slowlyness, but my shitty coding abilities. Cuold tou please = tell me the weaknesse of the code?

It's not shitty coding (though I do f= ind your code unreadable). It's because C is compiled into assembly, wh= en you tell it to iterate that many times, it can put the number directly i= nto a register and decrement the value. Ruby is much higher level than that= . Numbers are actual objects that must be allocated, for example. So an exa= mple like this, C is going to dramatically outperform Ruby.

Like anything, there are tradeoffs. For instance, by represe= nting numbers with just binary values that fit into C's int type, you y= our program up to the limitations of that type (e.g. in Ruby you can have a= rbitrarily large numbers `$ruby -e 'p 2**2000'`).

And, of course, the Ruby version uses a closure. ANSI C= doesn't have closures, which prevents it from being able to support ve= ry powerful coding styles (i.e. many of the things Lisp is famous for).

So, really its best to figure out what your domain is a= nd find a language well suited to that. If your domain is hardcore number c= runching, then Ruby is probably not the best choice (though I did solve a n= umber of Project Euler problems with it).

-Josh
--f46d0444ee2d99cc3c04bc00d9ad--