From: Niklas Frykholm Date: 2002-07-01T01:48:42+09:00 Subject: Re: select loop question [Joseph McDonald]: > NF> Just do > NF> answer[0].each {|r| readfds.delete(r)} > > Thanks, I do notice that Array.delete seems to be very slow, If I run this: > > hits = (200..300).to_a > 1.upto(1000) do > > arr = [] > 1.upto(500) do |x| > arr.push(x) > end > > # hits.each do |h| > # arr.delete(h) > # end > > end > > I get: > 0.47 real 0.46 user 0.00 sys > If I uncomment out the delete portion I get: > 8.74 real 8.23 user 0.00 sys > > My guess is that it is doing the cartesion product thing, interesting > that it is 8 times slower than: > > arr.each do |x| > if x >= 200 and x <= 300 > arr.slice!(x) > end > end > > which gives: 1.09 real 1.06 user 0.00 sys If you do many deletes, it is slow. It has to search the array for the element to delete and then move all the data that follows it. Note that slice! is not the same thing, when you do slice!(x) you remove the element at position x, not the element with content x. So the array does not have to be searched. If you want to do fast deletes based on content, you can use a hash to store the data. Store the data in the key of the hash and a dummy value such as "true" in the value. You get fast lookups and there is no need to move elements. // Niklas