From 46188608c534aa585ad346792dd60897a7ef1b98 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Thu, 1 Aug 2002 09:22:29 +0000 Subject: [PATCH] Pushing stuff onto an array is /way/ more efficient than += ing it. Some measurements tell me it's around 140 times faster. --- trunk/ripnews/news/article.rb | 37 ++++++++++----------- trunk/ripnews/set/intspan.rb | 60 +++++++++++++++++------------------ 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/trunk/ripnews/news/article.rb b/trunk/ripnews/news/article.rb index 48854dc..2b35d02 100644 --- a/trunk/ripnews/news/article.rb +++ b/trunk/ripnews/news/article.rb @@ -67,10 +67,10 @@ def add(messid, id, server, subject) # print "Id: #{id}\n" # print "Server: #{server}\n" # print "Subject: #{subject}\n" - @messids += [messid] - @ids += [id.to_i] - @servers += [server] - @subjects += [subject] + @messids.push(messid) + @ids.push(id.to_i) + @servers.push(server) + @subjects.push(subject) @sorted = false @grouped = false end @@ -384,6 +384,7 @@ end def group_is_complete(subj) group_subjects unless @grouped + #print "Subject: #{subj}\n" print "length: #{@groups[subj]["messages"].length} total: #{@groups[subj]["total"].to_i}\n" if Debuglevel > 1 umessids = @groups[subj]["messages"].uniq if (umessids.length ) >= @groups[subj]["total"].to_i @@ -414,7 +415,7 @@ def group_subjects for i in (0...@subjects.length) print "group subjects: #{i} #{@subjects[i]}\n" if Debuglevel > 1 if @subjects[i] =~ /(.*)\((\d+)\/(\d+)\)(.*)/ || @subjects[i] =~ /(.*)\[(\d+)\/(\d+)\](.*)/ - j = "#{$1}#{$4}" + j = "#{$1}#{$4} (#{$3})" number = $2 total = $3 else @@ -422,12 +423,12 @@ def group_subjects number = 1 total = 1 end - if @groups.has_key?(j) and number.to_i !=0 - @groups[j]["messages"] += [ @messids[i] ] - @groups[j]["ids"] += [ @ids[i].to_i ] - @groups[j]["servers"] += [ @servers[i] ] - @groups[j]["subject"] += [ @subjects[i] ] - elsif number.to_i !=0 + if @groups.has_key?(j) and number.to_i != 0 + @groups[j]["messages"].push(@messids[i]) + @groups[j]["ids"].push(@ids[i].to_i) + @groups[j]["servers"].push(@servers[i]) + @groups[j]["subject"].push(@subjects[i]) + elsif number.to_i != 0 @groups[j] = {} @groups[j]["total"] = total @groups[j]["messages"] = [ @messids[i] ] @@ -492,7 +493,7 @@ def save_cache(cachedir) file = File.new( filename, "w" ) or print "couldn't open cachefile for writing\n" cache = [] for i in (0...@subjects.length) - cache += ["#{@ids[i]}|#{@messids[i]}|#{@servers[i]}|#{@subjects[i]}\n"] + cache.push("#{@ids[i]}|#{@messids[i]}|#{@servers[i]}|#{@subjects[i]}\n") end cache.sort! file.print cache @@ -944,12 +945,12 @@ def group_subject_sort(subj) print "subj sort #{@groups[subj]["messages"][i]}\n" if Debuglevel > 2 print "subj sort #{@groups[subj]["ids"][i]}\n" if Debuglevel > 2 print "subj sort #{@groups[subj]["servers"][i]}\n" if Debuglevel > 2 - sort_arr += [ [ + sort_arr.push( [ @groups[subj]["subject"][i].dup, @groups[subj]["messages"][i].dup, @groups[subj]["ids"][i].dup, @groups[subj]["servers"][i].dup - ] ] + ] ) end sort_arr.sort!{|a,b| r = ward_sort(a[0], b[0]) @@ -961,10 +962,10 @@ def group_subject_sort(subj) @groups[subj].clear sort_arr.collect{|i| if @groups[subj].has_key?("messages") - @groups[subj]["subject"] += [i[0]] - @groups[subj]["messages"] += [i[1]] - @groups[subj]["ids"] += [i[2]] - @groups[subj]["servers"] += [i[3]] + @groups[subj]["subject"].push(i[0]) + @groups[subj]["messages"].push(i[1]) + @groups[subj]["ids"].push(i[2]) + @groups[subj]["servers"].push(i[3]) else @groups[subj]["subject"] = [i[0]] @groups[subj]["messages"] = [i[1]] diff --git a/trunk/ripnews/set/intspan.rb b/trunk/ripnews/set/intspan.rb index ef4dc94..155ecf5 100644 --- a/trunk/ripnews/set/intspan.rb +++ b/trunk/ripnews/set/intspan.rb @@ -75,7 +75,7 @@ def _copy_array(array) # copies an array into @set if (edges.length > 0) and (edges[-1] == element-1) edges[-1] = element else - edges += [ element-1, element ] + edges.push(element-1, element) end end @@ -109,7 +109,7 @@ def _copy_run_list(runlist) print "#{i}\n" if Debuglevel > 0 begin if i =~ /^(-?\d+)$/x - edges += [ ($1.to_i-1), $1.to_i ] + edges.push(($1.to_i-1), $1.to_i) next end @@ -119,7 +119,7 @@ def _copy_run_list(runlist) print "Set::IntSpan::_copy_run_list: Bad order: #{runlist}\n" exit else - edges += [ ($1.to_i-1), $2.to_i ] + edges.push(($1.to_i-1), $2.to_i) next end end @@ -131,13 +131,13 @@ def _copy_run_list(runlist) exit end @set = {"negInf" => true} - edges += [ $1.to_i ] + edges.push($1.to_i) next end if i =~ /^(-?\d+)-\)$/x print "match rule 3\n" - edges += [ ($1.to_i-1) ] + edges.push(($1.to_i-1)) @set = {"posInf" => true} last = true next @@ -221,7 +221,7 @@ def run_list if edges.length > 0 edges = ['(', edges] if @set["negInf"] - edges += [')'] if @set["posInf"] + edges.push(')') if @set["posInf"] print edges.join("/"),"\n" if Debuglevel > 0 @@ -236,11 +236,11 @@ def run_list (upper.to_s <=> ')')!=0 and ((lower+1) == upper)) print "#{upper}\n" if Debuglevel > 0 - runs += [ "#{upper}" ] + runs.push("#{upper}") else lower += 1 if (lower.to_s <=> "(")!=0 print "#{lower}-#{upper}\n" if Debuglevel > 0 - runs += [ "#{lower}-#{upper}" ] + runs.push("#{lower}-#{upper}") end end end @@ -295,22 +295,22 @@ def union(set_spec) if (xA < xB) iA += 1 inA = ! inA - not inB and eS += [ xA ] + not inB and eS.push(xA) elsif (xB < xA) iB += 1 inB = ! inB - not inA and eS += [ xB ] + not inA and eS.push(xB) else iA += 1 iB += 1 inA = ! inA inB = ! inB - inA == inB and eS += [ xA ] + inA == inB and eS.push(xA) end end - iA < eA.length and (! inB) and eS += eA[iA..eA.length] - iB < eB.length and (! inA) and eS += eB[iB..eB.length] + iA < eA.length and (! inB) and eS.push(eA[iA..eA.length]) + iB < eB.length and (! inA) and eS.push(eB[iB..eB.length]) s.set_pos_inf(@set["posInf"] || b.pos_inf) s.set_edges(eS) @@ -341,22 +341,22 @@ def intersect(set_spec) if (xA < xB) iA += 1 inA = ! inA - inB and eS += [ xA ] + inB and eS.push(xA) elsif (xB < xA) iB += 1 inB = ! inB - inA and eS += [ xB ] + inA and eS.push(xB) else iA += 1 iB += 1 inA = ! inA inB = ! inB - inA == inB and eS += [ xA ] + inA == inB and eS.push(xA) end end - iA < eA.length and inB and eS += eA[iA..eA.length] - iB < eB.length and inA and eS += eB[iB..eB.length] + iA < eA.length and inB and eS.push(eA[iA..eA.length]) + iB < eB.length and inA and eS.push(eB[iB..eB.length]) s.set_neg_inf(@set["posInf"] && b.pos_inf) s.set_edges(eS) @@ -386,22 +386,22 @@ def diff (set_spec) if (xA < xB) iA += 1 inA = ! inA - not inB and eS += [ xA ] + not inB and eS.push(xA) elsif (xB < xA) iB += 1 inB = ! inB - inA and eS += [ xB ] + inA and eS.push(xB) else iA += 1 iB += 1 inA = ! inA inB = ! inB - inA != inB and eS += [ xA ] + inA != inB and eS.push(xA) end end - iA < eA.length and not inB and eS += eA[iA..eA.length] - iB < eB.length and inA and eS += eB[iB..eB.length] + iA < eA.length and not inB and eS.push(eA[iA..eA.length]) + iB < eB.length and inA and eS.push(eB[iB..eB.length]) s.set_edges(eS) @@ -428,18 +428,18 @@ def xor(set_spec) if (xA < xB) iA += 1 - eS += [ xA ] + eS.push(xA) elsif (xB < xA) iB += 1 - eS += [ xB ] + eS.push(xB) else iA += 1 iB += 1 end end - iA < eA.length and eS += eA[iA..eA.length] - iB < eB.length and eS += eB[iB..eB.length] + iA < eA.length and eS.push(eA[iA..eA.length]) + iB < eB.length and eS.push(eB[iB..eB.length]) s.set_pos_inf(@set["posInf"] ^ b.pos_inf) s.set_edges(eS) @@ -607,7 +607,7 @@ def insert(n) end if n > edge[-1]+1 - @set["edges"] += [n-1, n] + @set["edges"].push(n-1, n) return elsif n > edge[-1] @set["edges"][-1] += 1 @@ -636,8 +636,8 @@ def insert(n) lower = edge[0...i] upper = edge[i...edge.length] edge = lower - edge += [n-1, n] - edge += upper + edge.push(n-1, n) + edge.push(upper) elsif (not lGap and rGap) edge[i-1] += 1 elsif ( lGap and not rGap)