
    z%i                         d Z ddlZddlZddlZddlZddlZddlZ ej                         Zd Z	d Z
d Zd Zd Zedk(  r e        yy)	z_Sort performance test.

See main() for command line syntax.
See tabulate() for output format.

    Nc                 b   t         j                  j                  t        d| z        }	 t	        |d      }t        j                  |      }|j                          t        d      D ]C  }t        j                  |       }|d| }|d|= |j                          |j                  |       ~E 	 t#        |      | k(  sJ |S # t        $ r t        j                  }t        |       D cg c]	  } |        nc c}w }}	 	 t	        |d      }t        j                  ||       |j                          d}|rS	 t        j                  |       n<# t        $ r Y n1w xY w# r&	 t        j                  |       w # t        $ r Y w w xY ww xY wn$# t        $ r}t!        d|d|       Y d}~nd}~ww xY wY w xY w)z+Return a list of n random floats in [0, 1).zrr%06drb
   Nwbzcan't write:)ospathjointdopenmarshalloadcloserangerandom	randrangereverseextendOSErrordumpunlinkprintlen)nfnfpresultitemprmsgs           A/home/uftp/.pyenv/versions/3.12.0/lib/python3.12/test/sortperf.py
randfloatsr#      s   
 
b(Q,	'B"d^& b!

r 	A  #A"1:Drr
LLNMM$	 v;!M;  /MM$Qx(!!#(((	/
"d^VR(
		""  		""  
  	/-S#..	//s   B> >&F.$C32F.<4E0F3E	F		EFEFFE21F2	E>	;F=E>	>FFF.	F'F"F."F''F.-F.c                  @    t         j                  j                          y N)sysstdoutflush     r"   r(   r(   8   s    JJr*   c                     t        j                         }| j                          t        j                         }t        d||z
  z  d       t	                y )Nz%6.2f end)timeperf_countersortr   r(   )Lt0t1s      r"   doitr5   ;   s?    				BFFH				B	'RU
%	Gr*   c                    t        dD cg c]  }|dz   	 c}      }ddt        |      z  z   }t        |d|z   z         | D ]  }d|z  }t        |      }t        d||fz  d	       t	                t        |       |j                          t        |       t        |       t        d
      D ]=  }t        j                  |      }t        j                  |      }	||	   ||   c||<   ||	<   ? t        |       |dk\  r.t        d      D cg c]  }t        j                          c}|dd t        |       t        |dz        D ],  }t        j                         |t        j                  |      <   . t        |       |dkD  r#|dd= ||dz  z  }t        t        d |            }t        |       ~t        t        t        dg|z              }t        |       ~|dz  }
t        t        |
dz
  dd            }|j                  t        |
             t        t        t        |            }t        |       t                 yc c}w c c}w )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

    z	*\/3+%~=!r1   z%2s %7sz %6s)r   z2**i   z%2d %7dr,   r-      r   iNd      c                 
    |   S r%   r)   )xs    r"   <lambda>ztabulate.<locals>.<lambda>y   s
    QB3 r*   g         )tupler   r   r#   r(   r5   r   r   r   r   listmapabsr   float)r    chcasesfmtr   r   r2   dummyi1i2halfs              r"   tabulaterL   B   s   & 622;67Evc%j((C	#&
'( 4FqMi1a& c*Q			QQ 1X 	(E!!!$B!!!$BR5!B%LAbE1R5	( 	Q 749"I>5v}}>AcdGQ 18_ 	5E%+]]_Afq!"	5Q q5!"Q!VA S*+AQ S4&1*%&Q
 AvtaxR()	t UAQi4 7, ?s   I
I
c                     d} d}t         j                  dd rt        t         j                  d         x} }t         j                  dd rot        t         j                  d         }t         j                  dd r@d}t         j                  dd D ]  }d|z  t        |      z   } t	        j
                  |       t        | |dz         }t        |       y)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.

          r7   Nr>   r8   i )r&   argvinthashr   seedr   rL   )k1k2r<   ar    s        r"   mainrW      s     
B	B
xx|chhqk""R88AB<SXXa[!Bxx|!" ,A	DG+A,Ab"Q$AQKr*   __main__)__doc__r&   r/   r   r   tempfiler   
gettempdirr   r#   r(   r5   rL   rW   __name__r)   r*   r"   <module>r]      s^         	X%NJX4 zF r*   