o
    GXh                     @   sz   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r;e  dS dS )z_Sort performance test.

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

    Nc                    sf  t jtd|  }zt|d}W nn ty~   tj  fddt| D }z=z&t|d}t	|| |
  d}W |rLzt | W n tyK   Y nw n|r`zt | W w  ty_   Y w w w W n ty{ } ztd|d| W Y d}~nd}~ww Y n+w t|}|
  td	D ]}t| }|d| }|d|= |  || ~qt|| ksJ |S )
z+Return a list of n random floats in [0, 1).zrr%06drbc                    s   g | ]}  qS  r   ).0irr   7/opt/python-3.10.19/usr/lib/python3.10/test/sortperf.py
<listcomp>   s    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   sV   




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
   
r2   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 ]}t	|}t	|}|| || ||< ||< qFt| |dkrudd tdD |dd< t| t|d D ]}t |t	|< qt| |dkr|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  qd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       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   r3   iNd      c                 S   s   |   S r%   r   )xr   r   r   <lambda>y   s    ztabulate.<locals>.<lambda>g         )tupler   r   r$   r(   r2   r   r   r   r   listmapabsr   float)
r   casesfmtr   r   r/   r6   Zi1Zi2Zhalfr   r   r   tabulateB   sN   


rD   c                  C   s   d} d}t jdd rBtt jd  } }t jdd rBtt jd }t jdd rBd}t jdd D ]
}d| t| }q2t| 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   rD   )Zk1Zk2r9   ar   r   r   r   main   s   	
rL   __main__)__doc__r&   r,   r   r   tempfiler   
gettempdirr   r$   r(   r2   rD   rL   __name__r   r   r   r   <module>   s     'L
