From: "Shot (Piotr Szotkowski)" Date: 2009-11-03T21:46:50+09:00 Subject: Ruby Inline over two times slower under 1.9 than under 1.8? --gr/z0/N6AeWAPJVB Content-Type: multipart/mixed; boundary="IrhDeMKUP4DT/M7F" Content-Disposition: inline --IrhDeMKUP4DT/M7F Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I=E2=80=99m starting to dip my toes in non-Ruby coding (for performance rea= sons) and began with looking at the (simplest) choice of Ruby Inline. Interestingly, while my own number-crunching code seems to be generally three times faster in Ruby 1.9 than in Ruby 1.8, the attached script =E2=80=93 a slightly altered Corey Hoffstein=E2=80=99s example=C2=B9 =E2=80= =93 seems to be actually running its Ruby part slightly slower under Ruby 1.9, and over two times slower when it comes to its C part: $ ruby -v sort.rb; ruby sort.rb; ruby sort.rb ruby 1.8.7 (2009-09-11 patchlevel 202) [x86_64-linux] Ruby Time: 68.987515 Inline Time: 7.591179 Ruby Time: 67.758257 Inline Time: 7.371522 Ruby Time: 68.420539 Inline Time: 7.361452 $ ruby -v sort.rb; ruby sort.rb; ruby sort.rb ruby 1.9.1p243 (2009-07-16) [x86_64-linux] Ruby Time: 80.945370503 Inline Time: 17.813790662 Ruby Time: 75.493231541 Inline Time: 16.430632733 Ruby Time: 74.406748965 Inline Time: 17.018331102 What could be the culprit of this speed difference? (Note that the C part is using the C++ Boost library =E2=80=93 but I doubt = this matters much, as the code is the same in both cases, and the resulting =2Eso file seems to be interchaneable between the two Ruby versions.) =C2=B9 http://coreyhoffstein.com/2009/06/16/sorting-with-ruby-rubyinline-an= d-thrust/ =E2=80=94 Shot --=20 ruby.about.com thinks =E2=80=98trollop=E2=80=99 is an inappropriate name for a gem? Wait till they meet me in person. [William Morgan] --IrhDeMKUP4DT/M7F Content-Type: application/x-ruby Content-Disposition: attachment; filename="sort.rb" Content-Transfer-Encoding: quoted-printable # Ruby Sort Neighbors example=0A# Take N random (x,y) points=0A# For each= point, sort the neighbors!=0A=0Arequire 'rubygems'=0Arequire 'inline'=0A= =0AN =3D 1_000 =0A=0Aclass Point=0A attr_accessor :x, :y=0A=0A def initia= lize(x, y)=0A @x =3D x=0A @y =3D y=0A end=0A=0A def distance(other)= =0A Math.sqrt((other.x - @x)**2 + (other.y - @y)**2)=0A end=0Aend=0A=0A= def find_nearest_neighbors(point, neighbors)=0A return neighbors.sort { |a= ,b| point.distance(a) <=3D> point.distance(b) }=0Aend=0A=0Aclass NearestNei= ghbor=0A inline(:C) do |builder|=0A builder.add_compile_flags '-x c++',= '-lstdc++', '-I/usr/local/include/boost-1_39/'=0A builder.include ''=0A builder.include ''=0A builder.include ''=0A builder.include ''=0A=0A builder.prefix '=0A i= nt distance(VALUE a, VALUE b, VALUE c) {=0A double a_x =3D NUM2DBL(r= b_iv_get(a, "@x"));=0A double a_y =3D NUM2DBL(rb_iv_get(a, "@y"));= =0A double b_x =3D NUM2DBL(rb_iv_get(b, "@x"));=0A double b_y= =3D NUM2DBL(rb_iv_get(b, "@y"));=0A double c_x =3D NUM2DBL(rb_iv_ge= t(c, "@x"));=0A double c_y =3D NUM2DBL(rb_iv_get(c, "@y"));=0A=0A = double b_dist =3D sqrt(pow(b_x-a_x, 2) + pow(b_y-a_y, 2));=0A d= ouble c_dist =3D sqrt(pow(c_x-a_x, 2) + pow(c_y-a_y, 2));=0A=0A retu= rn (b_dist < c_dist);=0A }=0A '=0A=0A builder.c '=0A VALUE = find_nearest_neighbors(VALUE point, VALUE neighbors) {=0A const VALU= E *base =3D RARRAY_PTR(neighbors);=0A std::vector sorted_neig= hbors(base, base + RARRAY_LEN(neighbors));=0A=0A std::sort(sorted_ne= ighbors.begin(), sorted_neighbors.end(), boost::bind(&distance, point, _1, = _2));=0A=0A return rb_ary_new4(RARRAY_LEN(neighbors), &sorted_neighb= ors.front());=0A }=0A '=0A end=0Aend=0A=0A# initialize our points= =0Apoints =3D []=0AN.times {=0A points << Point.new(rand, rand)=0A}=0A=0An= earest_neighbors =3D []=0Astart =3D Time.now=0Apoints.each_index { |i|=0A = nearest_neighbors[i] =3D find_nearest_neighbors(points[i], points[0...i] + = points[i+1..(N-1)])=0A}=0Afinish =3D Time.now=0Aputs "Ruby Time: #{finish-s= tart}"=0A=0Anearest_neighbors_inline =3D []=0Ann =3D NearestNeighbor.new()= =0Astart =3D Time.now=0Apoints.each_index { |i|=0A nearest_neighbors_inlin= e[i] =3D nn.find_nearest_neighbors(points[i], points[0...i] + points[i+1..(= N-1)])=0A}=0Afinish =3D Time.now=0Aputs "Inline Time: #{finish-start}"=0A --IrhDeMKUP4DT/M7F-- --gr/z0/N6AeWAPJVB Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkrwJjUACgkQi/mCfdEo8Uo0tQCgx3E6GUal7SKL+JskB84lN3nP XSoAn3xRq2SpjWyOZOaSBwOOh58gtmJM =m2g1 -----END PGP SIGNATURE----- --gr/z0/N6AeWAPJVB--