f _@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. Ncshtjtd|}zt|d}Wntytjfddt|D}zxzJt|d}t ||| d}W|rzt |Wn:tyYn*0|rzt |W0tyY000Wn2ty}zt d|d|WYd}~n d}~00YnX0t |}| td D]:}t|}|d|}|d|=|||~qt||ksdJ|S) z+Return a list of n random floats in [0, 1).zrr%06drbcsg|] }qSr).0irr&/usr/lib64/python3.10/test/sortperf.py zrandfloats..wbNz can't write: )ospathjointdopenOSErrorrandomrangemarshaldumpcloseunlinkprintload randrangereverseextendlen)nfnfpresultmsgrZtemprrr randfloatssF      *      r%cCstjdSN)sysstdoutflushrrrrr)8sr)cCs6t}|t}td||ddtdS)Nz%6.2f end)time perf_countersortrr))Lt0t1rrrdoit;s r3c CstdddD}ddt|}t|d||D]}d|>}t|}td||fd d tt||t|t|td D]2}t |}t |}||||||<||<qt||d krd dtd D|dd<t|t|dD]}t|t |<qt||dkrT|dd=||d}t t dd|}t|~t t t dg|}t|~|d} t t| ddd}| t| t t t|}t|tq6dS)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)rZchrrrr Ur ztabulate..z *\/3+%~=!z%2s %7sz %6s)rz2**iz%2d %7dr*r+r cSsg|] }tqSr)r)rdummyrrrr kr iNdcSs| Sr&r)xrrryr ztabulate..g)tuplerrr%r)r3rrrrlistmapabsrfloat) rZcasesfmtrr r0r6Zi1Zi2ZhalfrrrtabulateBsL       rCcCsd}d}tjddrttjd}}tjddrttjd}tjddrd}tjddD]}d|t|}qdt|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. r4Nr;r5i )r'argvinthashrseedrrC)Zk1Zk2r9arrrrmains  rK__main__)__doc__r'r-rrtempfiler gettempdirrr%r)r3rCrK__name__rrrrs'L