[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: optimizing for speed - Array#each

Ryan Davis

5/24/2007 6:05:00 PM


On May 23, 2007, at 14:59 , Mike Steiner wrote:

> I ran the profiler on my program and it said that 51% of the time
> is spent
> in Array#each. Is there any way to speed this up (like using
> Array#each_index perhaps)?

The time spent on Array#each itself is minimal and can't really be
sped up much. What you're really seeing is the time spent in the
block's code. If you have a bunch of each calls all over, you can
refactor each one to call out to a method:

ary.each { ... } => def x; ...; end; ary.each { x }

That way you can get better accounting on which block is taking up
the most time.

> And is there a quicker way to do a .gsub if I'm using just strings
> and not a
> regexp?

Not by much. I will say (based on a subsequent email of yours on this
thread) that you shouldn't have to be doing these gsubs just because
you're on windows. IIRC, ruby should open files with either form of
path separator.

> By the way, the program compares 2 databases and determines which
> records
> have been inserted and deleted, and each database has about 15,000
> records
> to compare.

15k records to compare isn't that much, honestly.

One thing I would suggest is for you to use an alternative profiler.
shugo's prof or my zenprofile gives much more accurate profiles where
lots and lots of method calls are involved. The beauty of the default
profiler is how small and easy it is to write. The pain is that it is
biased towards # of calls, not time spent.