2008/9/16 Daniel Berger <djberg96 / gmail.com>:
> I recently converted a C extension to get process table information on
> Solaris into a pure Ruby. I knew it would be slower, I just didn't
> realize how _much_ slower it would be. I was expecting the pure Ruby
> version to be about 1/10th as fast. Instead, it's about 1/70th as
> fast. Anticipating the, "Is it fast enough?" question, my answer is,
> "I'm not sure". Besides, tuning can be fun. :)
>
> Anyway, below is the code. I ran it through the profiler, but the top
> two most costly ops were Dir.foreach, which I don't see any way to
> optimize*, and the loop that gathers environment information, which I
> again see no way to optimize.

You can optimize the loop body.  Profiler output takes a while to get used to.

Few things that caught my attention:

You can combine the first two "next" in one.  Also matching with the
RX on the left side is faster AFAIK.

Try to use as few psinfo.unpack as possible, i.e. ideally only 1.

Use the block form of File.open.

Do not use sysread/syswrite/sysseek unless you have to (most of the
time you don't).

You can replace all but the first checks for block_given? with
"array". Might be faster.

Have fun!

robert

-- 
use.inject do |as, often| as.you_can - without end