B /g^c@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. iNcs6tjtd|}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%06dZrbcsg|] }qSr).0irr%/usr/lib64/python3.7/test/sortperf.py zrandfloats..ZwbNz can't writez: )osZpathZjointdZopenZOSErrorrandomrangemarshalZdumpZcloseZunlinkprintZload randrangereverseextend)nZfnZfpZresultZmsgrZtemprrr randfloatss:   &     rcCstjdSN)sysZstdoutflushrrrrr8srcCs6t}|t}td||ddtdS)Nz%6.2f Zend)timeZ perf_countersortrr)LZt0Zt1rrrdoit;s rc 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)rr)rZchrrrrUrztabulate..z *\/3+%~=!z%2s %7sz %6s)rz2**iz%2d %7drrrcSsg|] }tqSr)r )rdummyrrrrkriNidicSs| Srr)xrrrZysztabulate..gi)ZtupleZlenrrrrrr r rZlistZmapZabsrZfloat) rZcasesZfmtrrrrZi1Zi2ZhalfrrrtabulateBsL     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. iirNr!ri )rZargvZintZhashr Zseedr r")Zk1Zk2r Zarrrrmains  r#Z__main__)Z__doc__rrr r Ztempfiler Z gettempdirr rrrr"r#Z__name__rrrrZs'L