From: John Carter Date: 2007-05-25T07:34:34+09:00 Subject: Re: optimizing for speed - Array#each On Thu, 24 May 2007, 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)? Mostly what the profiler is telling you is "don't do that". Which sometimes isn't very helpful. The thing to do rather than ask "is there something faster than Array#each?" rather ask is there a way I can rewrite my algorithm so I don't called Array#each so much? I tend to not look at those entries in the profiler output and skip downwards until I find the first method I wrote and optimize that. > And is there a quicker way to do a .gsub if I'm using just strings and not a > regexp? I don't know exactly what you're doing but String []= does a lot more than I would naively think. ------------------------------------------------------------- String#[]= str[fixnum] = fixnum str[fixnum] = new_str str[fixnum, fixnum] = new_str str[range] = aString str[regexp] = new_str str[regexp, fixnum] = new_str str[other_str] = new_str ------------------------------------------------------------------------ Element Assignment---Replaces some or all of the content of _str_. The portion of the string affected is determined using the same criteria as +String#[]+. If the replacement string is not the same length as the text it is replacing, the string will be adjusted accordingly. If the regular expression or string is used as the index doesn't match a position in the string, +IndexError+ is raised. If the regular expression form is used, the optional second +Fixnum+ allows you to specify which portion of the match to replace (effectively using the +MatchData+ indexing rules. The forms that take a +Fixnum+ will raise an +IndexError+ if the value is out of range; the +Range+ form will raise a +RangeError+, and the +Regexp+ and +String+ forms will silently ignore the assignment. > 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. Have a look at http://rubygarden.org:3000/Ruby/page/show/RubyOptimization for more info. If you are still stuck, and it doesn't reveal any confidential details, send the top 50 lines profiler output to the group and ask for suggestions. John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand