B /g^@svdZddlZddlZddlZddlZddlZddlZeZddZ ddZ ddZ d d Z d d Z ed krre dS)z_Sort performance test. See main() for command line syntax. See tabulate() for output format. Ncs6tjtd|}yt|d}Wntk rtjfddt|D}yVz&t|d}t ||| d}Wd|ryt |Wntk rYnXXWn0tk r}zt d|d|Wdd}~XYnXYnZXt |}| xDtd D]8}t|}|d|}|d|=|||~qW|S) z+Return a list of n random floats in [0, 1).zrr%06drbcsg|] }qSr).0i)rr%/usr/lib64/python3.7/test/sortperf.py szrandfloats..wbNz can't write: )ospathjointdopenOSErrorrandomrangemarshaldumpcloseunlinkprintload randrangereverseextend)nfnfpresultmsgrZtempr)rr randfloatss:   &     r"cCstjdS)N)sysstdoutflushrrrrr%8sr%cCs6t}|t}td||ddtdS)Nz%6.2f )end)timeZ perf_countersortrr%)LZt0Zt1rrrdoit;s r+c CstdddD}ddt|}t|d|x|D]}d|>}t|}td||fd d tt||t|t|x>td D]2}t |}t |}||||||<||<qWt||d krd dtd D|dd<t|x(t|dD]}t|t |<q Wt||dkr`|dd=||d}t t dd|}t|~t t t dg|}t|~|d} t t| ddd}| t| t t t|}t|tq:WdS)a:Tabulate sort speed for lists of various sizes. The sizes are 2**i for i in r (the argument, a list). The output displays i, 2**i, and the time to sort arrays of 2**i floating point numbers with the following properties: *sort: random data \sort: descending data /sort: ascending data 3sort: ascending, then 3 random exchanges +sort: ascending, then 10 random at the end %sort: ascending, then randomly replace 1% of the elements w/ random values ~sort: many duplicates =sort: all equal !sort: worst case scenario cSsg|] }|dqS)r)r)rZchrrrrUsztabulate..z *\/3+%~=!z%2s %7sz %6s)rz2**iz%2d %7dr&)r'r cSsg|] }tqSr)r)rdummyrrrrksiNdcSs| S)Nr)xrrryztabulate..g)tuplelenrr"r%r+rrrrlistmapabsrfloat) rZcasesZfmtrrr*r.Zi1Zi2ZhalfrrrtabulateBsL     r<cCsd}d}tjddrttjd}}tjddrttjd}tjddrd}x&tjddD]}d|t|}qfWt|t||d}t|dS)zMain program when invoked as a script. One argument: tabulate a single row. Two arguments: tabulate a range (inclusive). Extra arguments are used to seed the random generator. r,Nr4r-i )r#argvinthashrZseedrr<)Zk1Zk2r1arrrrmains  rC__main__)__doc__r#r(rrZtempfiler Z gettempdirrr"r%r+r<rC__name__rrrrs'L