Pushing stuff onto an array is /way/ more efficient than += ing it. Some

measurements tell me it's around 140 times faster.
This commit is contained in:
Ward Wouts 2002-08-01 09:22:29 +00:00
parent 941d145f5a
commit 46188608c5
2 changed files with 49 additions and 48 deletions

View file

@ -67,10 +67,10 @@ def add(messid, id, server, subject)
# print "Id: #{id}\n" # print "Id: #{id}\n"
# print "Server: #{server}\n" # print "Server: #{server}\n"
# print "Subject: #{subject}\n" # print "Subject: #{subject}\n"
@messids += [messid] @messids.push(messid)
@ids += [id.to_i] @ids.push(id.to_i)
@servers += [server] @servers.push(server)
@subjects += [subject] @subjects.push(subject)
@sorted = false @sorted = false
@grouped = false @grouped = false
end end
@ -384,6 +384,7 @@ end
def group_is_complete(subj) def group_is_complete(subj)
group_subjects unless @grouped group_subjects unless @grouped
#print "Subject: #{subj}\n"
print "length: #{@groups[subj]["messages"].length} total: #{@groups[subj]["total"].to_i}\n" if Debuglevel > 1 print "length: #{@groups[subj]["messages"].length} total: #{@groups[subj]["total"].to_i}\n" if Debuglevel > 1
umessids = @groups[subj]["messages"].uniq umessids = @groups[subj]["messages"].uniq
if (umessids.length ) >= @groups[subj]["total"].to_i if (umessids.length ) >= @groups[subj]["total"].to_i
@ -414,7 +415,7 @@ def group_subjects
for i in (0...@subjects.length) for i in (0...@subjects.length)
print "group subjects: #{i} #{@subjects[i]}\n" if Debuglevel > 1 print "group subjects: #{i} #{@subjects[i]}\n" if Debuglevel > 1
if @subjects[i] =~ /(.*)\((\d+)\/(\d+)\)(.*)/ || @subjects[i] =~ /(.*)\[(\d+)\/(\d+)\](.*)/ if @subjects[i] =~ /(.*)\((\d+)\/(\d+)\)(.*)/ || @subjects[i] =~ /(.*)\[(\d+)\/(\d+)\](.*)/
j = "#{$1}#{$4}" j = "#{$1}#{$4} (#{$3})"
number = $2 number = $2
total = $3 total = $3
else else
@ -423,10 +424,10 @@ def group_subjects
total = 1 total = 1
end end
if @groups.has_key?(j) and number.to_i != 0 if @groups.has_key?(j) and number.to_i != 0
@groups[j]["messages"] += [ @messids[i] ] @groups[j]["messages"].push(@messids[i])
@groups[j]["ids"] += [ @ids[i].to_i ] @groups[j]["ids"].push(@ids[i].to_i)
@groups[j]["servers"] += [ @servers[i] ] @groups[j]["servers"].push(@servers[i])
@groups[j]["subject"] += [ @subjects[i] ] @groups[j]["subject"].push(@subjects[i])
elsif number.to_i != 0 elsif number.to_i != 0
@groups[j] = {} @groups[j] = {}
@groups[j]["total"] = total @groups[j]["total"] = total
@ -492,7 +493,7 @@ def save_cache(cachedir)
file = File.new( filename, "w" ) or print "couldn't open cachefile for writing\n" file = File.new( filename, "w" ) or print "couldn't open cachefile for writing\n"
cache = [] cache = []
for i in (0...@subjects.length) 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 end
cache.sort! cache.sort!
file.print cache 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]["messages"][i]}\n" if Debuglevel > 2
print "subj sort #{@groups[subj]["ids"][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 print "subj sort #{@groups[subj]["servers"][i]}\n" if Debuglevel > 2
sort_arr += [ [ sort_arr.push( [
@groups[subj]["subject"][i].dup, @groups[subj]["subject"][i].dup,
@groups[subj]["messages"][i].dup, @groups[subj]["messages"][i].dup,
@groups[subj]["ids"][i].dup, @groups[subj]["ids"][i].dup,
@groups[subj]["servers"][i].dup @groups[subj]["servers"][i].dup
] ] ] )
end end
sort_arr.sort!{|a,b| sort_arr.sort!{|a,b|
r = ward_sort(a[0], b[0]) r = ward_sort(a[0], b[0])
@ -961,10 +962,10 @@ def group_subject_sort(subj)
@groups[subj].clear @groups[subj].clear
sort_arr.collect{|i| sort_arr.collect{|i|
if @groups[subj].has_key?("messages") if @groups[subj].has_key?("messages")
@groups[subj]["subject"] += [i[0]] @groups[subj]["subject"].push(i[0])
@groups[subj]["messages"] += [i[1]] @groups[subj]["messages"].push(i[1])
@groups[subj]["ids"] += [i[2]] @groups[subj]["ids"].push(i[2])
@groups[subj]["servers"] += [i[3]] @groups[subj]["servers"].push(i[3])
else else
@groups[subj]["subject"] = [i[0]] @groups[subj]["subject"] = [i[0]]
@groups[subj]["messages"] = [i[1]] @groups[subj]["messages"] = [i[1]]

View file

@ -75,7 +75,7 @@ def _copy_array(array) # copies an array into @set
if (edges.length > 0) and (edges[-1] == element-1) if (edges.length > 0) and (edges[-1] == element-1)
edges[-1] = element edges[-1] = element
else else
edges += [ element-1, element ] edges.push(element-1, element)
end end
end end
@ -109,7 +109,7 @@ def _copy_run_list(runlist)
print "#{i}\n" if Debuglevel > 0 print "#{i}\n" if Debuglevel > 0
begin begin
if i =~ /^(-?\d+)$/x if i =~ /^(-?\d+)$/x
edges += [ ($1.to_i-1), $1.to_i ] edges.push(($1.to_i-1), $1.to_i)
next next
end end
@ -119,7 +119,7 @@ def _copy_run_list(runlist)
print "Set::IntSpan::_copy_run_list: Bad order: #{runlist}\n" print "Set::IntSpan::_copy_run_list: Bad order: #{runlist}\n"
exit exit
else else
edges += [ ($1.to_i-1), $2.to_i ] edges.push(($1.to_i-1), $2.to_i)
next next
end end
end end
@ -131,13 +131,13 @@ def _copy_run_list(runlist)
exit exit
end end
@set = {"negInf" => true} @set = {"negInf" => true}
edges += [ $1.to_i ] edges.push($1.to_i)
next next
end end
if i =~ /^(-?\d+)-\)$/x if i =~ /^(-?\d+)-\)$/x
print "match rule 3\n" print "match rule 3\n"
edges += [ ($1.to_i-1) ] edges.push(($1.to_i-1))
@set = {"posInf" => true} @set = {"posInf" => true}
last = true last = true
next next
@ -221,7 +221,7 @@ def run_list
if edges.length > 0 if edges.length > 0
edges = ['(', edges] if @set["negInf"] edges = ['(', edges] if @set["negInf"]
edges += [')'] if @set["posInf"] edges.push(')') if @set["posInf"]
print edges.join("/"),"\n" if Debuglevel > 0 print edges.join("/"),"\n" if Debuglevel > 0
@ -236,11 +236,11 @@ def run_list
(upper.to_s <=> ')')!=0 and (upper.to_s <=> ')')!=0 and
((lower+1) == upper)) ((lower+1) == upper))
print "#{upper}\n" if Debuglevel > 0 print "#{upper}\n" if Debuglevel > 0
runs += [ "#{upper}" ] runs.push("#{upper}")
else else
lower += 1 if (lower.to_s <=> "(")!=0 lower += 1 if (lower.to_s <=> "(")!=0
print "#{lower}-#{upper}\n" if Debuglevel > 0 print "#{lower}-#{upper}\n" if Debuglevel > 0
runs += [ "#{lower}-#{upper}" ] runs.push("#{lower}-#{upper}")
end end
end end
end end
@ -295,22 +295,22 @@ def union(set_spec)
if (xA < xB) if (xA < xB)
iA += 1 iA += 1
inA = ! inA inA = ! inA
not inB and eS += [ xA ] not inB and eS.push(xA)
elsif (xB < xA) elsif (xB < xA)
iB += 1 iB += 1
inB = ! inB inB = ! inB
not inA and eS += [ xB ] not inA and eS.push(xB)
else else
iA += 1 iA += 1
iB += 1 iB += 1
inA = ! inA inA = ! inA
inB = ! inB inB = ! inB
inA == inB and eS += [ xA ] inA == inB and eS.push(xA)
end end
end end
iA < eA.length and (! inB) and eS += eA[iA..eA.length] iA < eA.length and (! inB) and eS.push(eA[iA..eA.length])
iB < eB.length and (! inA) and eS += eB[iB..eB.length] iB < eB.length and (! inA) and eS.push(eB[iB..eB.length])
s.set_pos_inf(@set["posInf"] || b.pos_inf) s.set_pos_inf(@set["posInf"] || b.pos_inf)
s.set_edges(eS) s.set_edges(eS)
@ -341,22 +341,22 @@ def intersect(set_spec)
if (xA < xB) if (xA < xB)
iA += 1 iA += 1
inA = ! inA inA = ! inA
inB and eS += [ xA ] inB and eS.push(xA)
elsif (xB < xA) elsif (xB < xA)
iB += 1 iB += 1
inB = ! inB inB = ! inB
inA and eS += [ xB ] inA and eS.push(xB)
else else
iA += 1 iA += 1
iB += 1 iB += 1
inA = ! inA inA = ! inA
inB = ! inB inB = ! inB
inA == inB and eS += [ xA ] inA == inB and eS.push(xA)
end end
end end
iA < eA.length and inB and eS += eA[iA..eA.length] iA < eA.length and inB and eS.push(eA[iA..eA.length])
iB < eB.length and inA and eS += eB[iB..eB.length] iB < eB.length and inA and eS.push(eB[iB..eB.length])
s.set_neg_inf(@set["posInf"] && b.pos_inf) s.set_neg_inf(@set["posInf"] && b.pos_inf)
s.set_edges(eS) s.set_edges(eS)
@ -386,22 +386,22 @@ def diff (set_spec)
if (xA < xB) if (xA < xB)
iA += 1 iA += 1
inA = ! inA inA = ! inA
not inB and eS += [ xA ] not inB and eS.push(xA)
elsif (xB < xA) elsif (xB < xA)
iB += 1 iB += 1
inB = ! inB inB = ! inB
inA and eS += [ xB ] inA and eS.push(xB)
else else
iA += 1 iA += 1
iB += 1 iB += 1
inA = ! inA inA = ! inA
inB = ! inB inB = ! inB
inA != inB and eS += [ xA ] inA != inB and eS.push(xA)
end end
end end
iA < eA.length and not inB and eS += eA[iA..eA.length] iA < eA.length and not inB and eS.push(eA[iA..eA.length])
iB < eB.length and inA and eS += eB[iB..eB.length] iB < eB.length and inA and eS.push(eB[iB..eB.length])
s.set_edges(eS) s.set_edges(eS)
@ -428,18 +428,18 @@ def xor(set_spec)
if (xA < xB) if (xA < xB)
iA += 1 iA += 1
eS += [ xA ] eS.push(xA)
elsif (xB < xA) elsif (xB < xA)
iB += 1 iB += 1
eS += [ xB ] eS.push(xB)
else else
iA += 1 iA += 1
iB += 1 iB += 1
end end
end end
iA < eA.length and eS += eA[iA..eA.length] iA < eA.length and eS.push(eA[iA..eA.length])
iB < eB.length and eS += eB[iB..eB.length] iB < eB.length and eS.push(eB[iB..eB.length])
s.set_pos_inf(@set["posInf"] ^ b.pos_inf) s.set_pos_inf(@set["posInf"] ^ b.pos_inf)
s.set_edges(eS) s.set_edges(eS)
@ -607,7 +607,7 @@ def insert(n)
end end
if n > edge[-1]+1 if n > edge[-1]+1
@set["edges"] += [n-1, n] @set["edges"].push(n-1, n)
return return
elsif n > edge[-1] elsif n > edge[-1]
@set["edges"][-1] += 1 @set["edges"][-1] += 1
@ -636,8 +636,8 @@ def insert(n)
lower = edge[0...i] lower = edge[0...i]
upper = edge[i...edge.length] upper = edge[i...edge.length]
edge = lower edge = lower
edge += [n-1, n] edge.push(n-1, n)
edge += upper edge.push(upper)
elsif (not lGap and rGap) elsif (not lGap and rGap)
edge[i-1] += 1 edge[i-1] += 1
elsif ( lGap and not rGap) elsif ( lGap and not rGap)