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