From: Cliff Rosson Date: 2013-03-06T01:32:56+09:00 Subject: Re: Extending Ruby. Little help or guidance if you are willing! --00504502bd0a2c52bf04d730067a Content-Type: text/plain; charset=ISO-8859-2 Content-Transfer-Encoding: quoted-printable Although I think the exercise of extending ruby in C is a good one so I'll probably proceed as is. Still there is something to be said for being aware of the performance different calls in ruby have so I'll probably just play around with both ways. On Tue, Mar 5, 2013 at 8:31 AM, Cliff Rosson wrote= : > Honestly, you could probably get really far by changing a few things abou= t >> the implementation here. I'm assuming you're calling this frequently, in >> which case, you're creating these arrays often, it might be faster to se= t >> them into a constant, which you can then return from the case statement. >> Also, the pins.include?(seg) is going to do a sequential search through = the >> arrays. You could possibly run some tests to see which keys are most >> frequent and place them at the beginning so they are found first. If you >> have control over the display object, you might be able to swap out the >> symbols with integers, allowing for you to be clever with bit mathematic= s. >> It could possibly be faster to use a hash or a set than to use an array = for >> your pins, but not totally confident given the smallness of the arrays. = In >> general, you should benchmark to see if any of these have more merit. > > > Interesting point. I think you are right. I'll try and experiment with > this today. > > > On Mon, Mar 4, 2013 at 11:30 PM, Bartosz Dziewo=F1ski wrote: > >> On Tue, 05 Mar 2013 06:25:50 +0100, Cliff Rosson >> wrote: >> >> inline(:C) do |builder| >>> builder.c ' >>> VALUE compare(VALUE sym) { >>> >>> ID id =3D rb_intern_str(rb_str_new2("**test")); >>> >>> if (id =3D=3D sym) return Qtrue; >>> return Qfalse; >>> } >>> >>> ... >>> >>> >>> puts compare("tes") >>> =3D> false >>> puts compare("test") >>> =3D> false >>> puts compare(:test) >>> =3D> false >>> >>> Any clues as to why :test fails? >>> >> >> You're comparing an 'ID' value with a 'VALUE' value; these are not the >> same. VALUE represents a Ruby object (any object); ID is an internal >> representation of a method or variable name. >> >> You can convert an ID to a VALUE representing the corresponding symbol >> using rb_id2name(): >> >> ID id =3D rb_id2name(rb_intern_str(rb_**str_new2("test"))); >> >> Then it should work. (Untested.) >> >> -- >> Matma Rex >> >> > > > -- > vizualize.me/cliffrosson > --=20 vizualize.me/cliffrosson --00504502bd0a2c52bf04d730067a Content-Type: text/html; charset=ISO-8859-2 Content-Transfer-Encoding: quoted-printable
Although I think the exercise of extending ruby in C is a = good one so I'll probably proceed as is. Still there is something to be= said for being aware of the performance different calls in ruby have so I&= #39;ll probably just play around with both ways.


On Tue, Mar 5= , 2013 at 8:31 AM, Cliff Rosson <cliff.rosson@gmail.com> wrote:
Honestly, you c= ould probably get really far by changing a few things about the implementat= ion here. I'm assuming you're calling this frequently, in which cas= e, you're creating these arrays often, it might be faster to set them i= nto a constant, which you can then return from the case statement. Also, th= e pins.include?(seg) is going to do a sequential search through the arrays.= You could possibly run some tests to see which keys are most frequent and = place them at the beginning so they are found first. If you have control ov= er the display object, you might be able to swap out the symbols with integ= ers, allowing for you to be clever with bit mathematics. It could possibly = be faster to use a hash or a set than to use an array for your pins, but no= t totally confident given the smallness of the arrays. In general, you shou= ld benchmark to see if any of these have more merit.

Interesting point. I think you are right. I'l= l try and=A0experiment=A0with this today.=A0


On Mon, M= ar 4, 2013 at 11:30 PM, Bartosz Dziewo=F1ski <matma.rex@gmail.com&g= t; wrote:
On Tue, 05 Mar 2013 06:25:50 +0100, Cli= ff Rosson <c= liff.rosson@gmail.com> wrote:

=A0 inline(:C) do |builder|
=A0 =A0 builder.c '
=A0 =A0 VALUE compare(VALUE sym) {

=A0 =A0 =A0 ID id =3D rb_intern_str(rb_str_new2("test"));<= br>
=A0 =A0 =A0 if (id =3D=3D sym) return Qtrue;
=A0 =A0 =A0 return Qfalse;
=A0 =A0 }

...


puts compare("tes")
=3D> false
puts compare("test")
=3D> false
puts compare(:test)
=3D> false

Any clues as to why :test fails?

You're comparing an 'ID' value with a 'VALUE' value; th= ese are not the same. VALUE represents a Ruby object (any object); ID is an= internal representation of a method or variable name.

You can convert an ID to a VALUE representing the corresponding symbol usin= g rb_id2name():

=A0 =A0 ID id =3D rb_id2name(rb_intern_str(rb_str_new2("test&qu= ot;)));

Then it should work. (Untested.)

--
Matma Rex




--



--
vizualize.me/cliffro= sson
--00504502bd0a2c52bf04d730067a--