#Timing test for "sort on fourth word"
#Specifically,two lines>=4 words will be sorted
#lexographically on the 4th,5th,etc..words
#Any line with fewer than four words will sorted to
# the end ,and will occur in "natural" order
import sys,string,time
wrerr = sys.stderr.write
#native custom sort
def fourth_word(ln1,ln2):
    lst1 = string.split(ln1)
    lst2 = string.split(ln2)
    #Compare "long" lines
    if(len(lst1)>=4 and len(lst2)>=4):
        return cmp(lst1[3:],lst2[3:])
    #Long lines before short lines
    elif(len(lst1)>=4 and len(lst2)<4):
        return -1
    #Short lines before long lines
    elif(len(ls1)<4 and len(lst2)>=4):
        return 1
    else:       #natural order
        return cmp(ln1,ln2)
#Dont count the read itself in the time
lines = open(sys.argv[1]).readlines()
#Time the custom comparison sort
start = time.time()
lines.sort(fourth_word)
end = time.time()
wrerr("Custom comparison func in %3.2f secs" % (end-start))

lines = open(sys.argv[1]).readlines()
#Time the schwartzian sort
start = time.time()
for n in range(len(lines)):
    lst = string.split(lines[n])
    if(len(lsr>=4)):
        lines[n] = (lst[3:],lines[n])
    else:
        lines[n] = (['\377'],lines[n])
lines.sort()

for n in range(len(lines)):
    lines[n] = lines[n][1]

end = time.time()
wrerr("Schwartzian transform sort in %3.2f secs" % (end-start))

posted on 2007-05-02 15:11  fenix  阅读(154)  评论(0编辑  收藏  举报