• schwartzian sort


    #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))

  • 相关阅读:
    K8S calico
    K9S之glusterfs
    kubeadm安装报错
    创建crd和cr
    分布式学习汇总
    容器常见问题分析
    项目迁移到k8s平台流程
    K8S storageclass
    awk命令
    K8S headless service服务详解
  • 原文地址:https://www.cnblogs.com/fenix/p/2335823.html
Copyright © 2020-2023  润新知