B
    18\                 @   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                sH  t jtd|  }yt|d}W n tk
r   tj  fddt| D }yVz&t|d}t	|| |
  d}W d|ryt | W n tk
r   Y nX X W n0 tk
r } ztd|d| W dd}~X Y nX Y nZX t|}|
  xDtd	D ]8}t| }|d| }|d|= |  || ~qW t|| ksDt|S )
z+Return a list of n random floats in [0, 1).zrr%06drbc                s   g | ]
}  qS  r   ).0i)rr   Q/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/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AssertionError)nfnfpresultmsgr   Ztempr   )r   r   
randfloats   s<    
&



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
    r0   c       
      C   s  t dd dD }ddt|  }t|d|   x| D ]}d|> }t|}td||f d	d
 t  t| |  t| t| x>tdD ]2}t	|}t	|}|| ||  ||< ||< qW t| |dkrdd tdD |dd< t| x(t|d D ]}t |t	|< q
W 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:W 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   chr   r   r   r   U   s    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   s    iNd      c             S   s   |   S )Nr   )xr   r   r   <lambda>y       ztabulate.<locals>.<lambda>g         )tupler   r   r$   r'   r0   r   r   r   r   listmapabsr   float)
r   casesfmtr   r   r-   r4   i1i2Zhalfr   r   r   tabulateB   sL    



rE   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}x&t jdd D ]}d| t| }qfW t| 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.

          r2   Nr:   r3   i )r%   argvinthashr   seedr   rE   )k1k2r7   ar   r   r   r   main   s    	
rO   __main__)__doc__r%   r*   r   r   tempfiler   
gettempdirr   r$   r'   r0   rE   rO   __name__r   r   r   r   <module>   s   'L