From: Josh Cheek Date: 2013-03-05T16:26:01+09:00 Subject: Re: Extending Ruby. Little help or guidance if you are willing! --f46d040890c17d8dc404d728626d Content-Type: text/plain; charset=ISO-8859-1 On Sat, Mar 2, 2013 at 7:31 PM, Cliff Rosson wrote: > Ruby -rprofile myprogram.rb shows that my display_segments method is the > culprit. I'm told that re-writing this in C could speed things up a bit so > I started playing around with ruby inline. Admittedly I seem to get lost in > the Ruby C API. > > I'm pretty much an amateur but I thought I would post here for some > guidance from any willing kind hearted soul. > > The culprit method is > > def display_segments(num, display, io, delay = 0.005) > power = display.power > seg_pins = display.segments > case num > when 0 > pins = [:a, :b, :c, :d, :e, :f] > when 1 > pins = [:b, :c] > when 2 > pins = [:a, :b, :g, :e, :d] > when 3 > end > seg_pins.each do |seg, pin| > pins.include?(seg) ? io.write(pin, LOW) : io.write(pin, HIGH) > end > io.write(power, HIGH) > sleep delay > io.write(power, LOW) > end > > Honestly, you could probably get really far by changing a few things about 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 set 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 mathematics. 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. --f46d040890c17d8dc404d728626d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Sat, Mar 2, 2013 at 7:31 PM, Cliff Rosson <cliff.rosson@gmail.com= > wrote:
Ruby -rprofile myprogram.rb shows that my display_segments method is the c= ulprit. I'm told that re-writing this in C could speed things up a bit = so I started playing around with ruby inline.=A0Admittedly=A0I seem to get = lost in the Ruby C API.

I'm pretty much an= =A0amateur=A0but I thought I would post here for some guidance from any wil= ling kind hearted soul.=A0

The culprit method is= =A0

def display_segments(num, display, io= , delay =3D 0.005)
=A0 po= wer =3D display.power
=A0= seg_pins =3D display.segments
=A0 case num
=A0 when 0
=A0 =A0 pins =3D [:a, :b, :c, :d, :e, :f]
=A0 when 1
=A0 =A0 pins =3D [:b, :c]
=A0 when 2
=A0 =A0 pins =3D [:a, :b, :g, :e, :d]
=
=A0 when 3
=A0 end
=A0 seg_pins.each do |seg, pin|
=A0 =A0 pins.include?(seg) ? io.write(pin, LO= W) : io.write(pin, HIGH)
=A0 end
=A0 io.write(power, HIGH)
=A0 sleep delay
=A0 io.write(power, LOW)
end


Honestly, you could probably get really far by changing a= few things about the implementation here. I'm assuming you're call= ing this frequently, in which case, you're creating these arrays often,= it might be faster to set them into a constant, which you can then return = from the case statement. Also, the pins.include?(seg) is going to do a sequ= ential 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 fo= und first. If you have control over the display object, you might be able t= o swap out the symbols with integers, allowing for you to be clever with bi= t mathematics. It could possibly be faster to use a hash or a set than to u= se 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 m= ore merit.
--f46d040890c17d8dc404d728626d--