a
    ze                     @   sv   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 Zdd Z	dd Z
dd Zd	d
 Zdd Zedkrre  dS )z_Sort performance test.

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

    Nc                    sj  t jtd|  }zt|d}W n ty   tj  fddt| D }zzzLt|d}t	|| |
  d}W |rzt | W q ty   Y q0 n(|rzt | W n ty   Y n0 0 W n2 ty } ztd|d| W Y d}~n
d}~0 0 Y nX0 t|}|
  td	D ]:}t| }|d| }|d|= |  || ~qt|| ksfJ |S )
z+Return a list of n random floats in [0, 1).zrr%06drbc                    s   g | ]
}  qS  r   ).0irr   #/usr/lib/python3.9/test/sortperf.py
<listcomp>       zrandfloats.<locals>.<listcomp>wbNzcan't write:
   )ospathjointdopenOSErrorrandomrangemarshaldumpcloseunlinkprintload	randrangereverseextendlen)nfnfpresultmsgr   Ztempr   r   r   
randfloats   sF    
*



r%   c                   C   s   t j  d S N)sysstdoutflushr   r   r   r   r)   8   s    r)   c                 C   s6   t  }|   t  }td||  dd t  d S )Nz%6.2f end)timeperf_countersortr   r)   )Lt0t1r   r   r   doit;   s
    r3   c           
      C   s  t dd dD }ddt|  }t|d|   | D ]}d|> }t|}td||f d	d
 t  t| |  t| t| tdD ]2}t	|}t	|}|| ||  ||< ||< qt| |dkrdd tdD |dd< t| t|d D ]}t |t	|< q t| |dkrT|dd= ||d  }t
tdd |}t| ~t
ttdg| }t| ~|d }	t
t|	d dd}|t|	 t
tt|}t| t  q6dS )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

    c                 S   s   g | ]}|d  qS )r/   r   )r   Zchr   r   r   r	   U   r
   ztabulate.<locals>.<listcomp>z	*\/3+%~=!z%2s %7sz %6s)r   z2**i   z%2d %7dr*   r+      r   c                 S   s   g | ]}t   qS r   )r   )r   dummyr   r   r   r	   k   r
   iNd      c                 S   s   |   S r&   r   )xr   r   r   <lambda>y   r
   ztabulate.<locals>.<lambda>g         )tupler   r   r%   r)   r3   r   r   r   r   listmapabsr   float)
r   Zcasesfmtr   r    r0   r6   Zi1Zi2Zhalfr   r   r   tabulateB   sL    




rC   c                  C   s   d} d}t jdd rtt jd  } }t jdd rtt jd }t jdd rd}t jdd D ]}d| t| }qdt| t| |d }t| dS )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.

          r4   Nr;   r5   i )r'   argvinthashr   seedr   rC   )Zk1Zk2r9   ar   r   r   r   main   s    	
rK   __main__)__doc__r'   r-   r   r   tempfiler   
gettempdirr   r%   r)   r3   rC   rK   __name__r   r   r   r   <module>   s   'L