From: Mike Steiner Date: 2007-05-25T00:14:06+09:00 Subject: Re: optimizing for speed - Array#each ------=_Part_80053_3014564.1180019646342 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline One of the things I'm doing is a lot of File.join's, and I have to do a gsub ( /\// , "\\" ) after each one (because I'm running under Windows). I can't use sets because I have duplicate records. There is no unique record key, so for each record (which I use as a key in a hash), I have an array listing the files it appears in (which can include the same file more than once). Thanks for the help! Mike Steiner On 5/24/07, Robert Klemme wrote: > > On 24.05.2007 00:15, Stefan Rusterholz wrote: > > 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)? > > Note that the profiler output is often irritating because time for each > also includes all the time spend in the block - and while #each is > invoked only once the block is invoked n times. > > >> And is there a quicker way to do a .gsub if I'm using just strings and > >> not a regexp? > > Quicker than what? Are you using the block form of gsub? Are you using > gsub! or gsub? Can you show some code? > > >> 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. > > 15,000 does not sound like much. What is the runtime of the program > without profile? > > And another idea: could be that you are using inefficient methods to > test for existence. Finding something in an Array is always O(n) unless > you have additional constraints; i.e. array content is sorted in which > case you can use binary search which is O(log(n)). It seems you want to > be doing set operations - so why don't you try to use Set for which > member tests are far more efficient (O(1) because it's using hashing > internally). > > Kind regards > > robert > > ------=_Part_80053_3014564.1180019646342--