;
Ëâ"Ic               @   s£   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 o e ƒ  n d S(	   u_   Sort performance test.

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

i    Nc                sª  t  j j t d |  ƒ } y t | d ƒ } Wnç t k
 oÛ t j ‰  ‡  f d †  t |  ƒ Dƒ } yq z3 t | d ƒ } t j	 | | ƒ | j
 ƒ  d } Wd | o/ y t  j | ƒ WqÑ t  j k
 o YqÑ Xn XWn: t k
 o. } z t d | d | ƒ WYd d } ~ Xn XYnx Xt j | ƒ } | j
 ƒ  xZ t d ƒ D]L } t j |  ƒ } | d | … } | d | … =| j ƒ  | j | ƒ ~ q<Wt | ƒ |  k p t ‚ | S(	   u+   Return a list of n random floats in [0, 1).u   rr%06du   rbc                s   g  } |  ] } | ˆ  ƒ  q
 S(    (    (   u   .0u   _[1]u   i(   u   r(    u*   /mit/python/lib/python3.0/test/sortperf.pyu
   <listcomp>   s    u   wbNu   can't writeu   :i
   (   u   osu   pathu   joinu   tdu   openu   IOErroru   randomu   rangeu   marshalu   dumpu   closeu   Noneu   unlinku   erroru   printu   loadu	   randrangeu   reverseu   extendu   lenu   AssertionError(   u   nu   fnu   fpu   resultu   msgu   iu   temp(    (   u   ru*   /mit/python/lib/python3.0/test/sortperf.pyu
   randfloats   s>    	

-
 
c               C   s   t  j j ƒ  d  S(   N(   u   sysu   stdoutu   flush(    (    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu   flush8   s    c             C   sE   t  j ƒ  } |  j ƒ  t  j ƒ  } t d | | d d ƒt ƒ  d  S(   Nu   %6.2fu   endu    (   u   timeu   clocku   sortu   printu   flush(   u   Lu   t0u   t1(    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu   doit;   s
    
c       
   	   C   sF  t  d „  d Dƒ ƒ } d d t | ƒ } t | d | ƒ x|  D]û} d | >} t | ƒ } t d | | f d	 d
 ƒt ƒ  t | ƒ | j ƒ  t | ƒ t | ƒ xO t d ƒ D]A } t j	 | ƒ } t j	 | ƒ } | | | | | | <| | <qµ Wt | ƒ | d k o$ d „  t d ƒ Dƒ | d d … <n t | ƒ x1 t | d ƒ D] } t j ƒ  | t j	 | ƒ <qPWt | ƒ | d k o1 | d d … =| | d } t
 d „  | ƒ } n t | ƒ ~ t
 t d g | ƒ } t | ƒ ~ | d }	 t |	 d d d ƒ } | j t |	 ƒ ƒ t
 t | ƒ } t | ƒ t ƒ  qC Wd S(   u:  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(   u   sort(    (   u   .0u   _[1]u   ch(    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu
   <listcomp>U   s    u	   *\/3+%~=!u   %2s %7su    %6su   iu   2**ii   u   %2d %7du   endu    i   i
   c             S   s!   g  } |  ] } | t  j  ƒ  q
 S(    (   u   random(   u   .0u   _[1]u   dummy(    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu
   <listcomp>k   s    iöÿÿÿNid   i   c             S   s   |  S(    (    (   u   x(    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu   <lambda>y   s    g      à¿i   iÿÿÿÿ(   u   iu   2**i(   u   tupleu   lenu   printu
   randfloatsu   flushu   doitu   reverseu   rangeu   randomu	   randrangeu   mapu   absu   extendu   float(
   u   ru   casesu   fmtu   iu   nu   Lu   dummyu   i1u   i2u   half(    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu   tabulateB   sR     




 !
$
 




c              C   sä   d }  d } t  j d d … o£ t t  j d ƒ }  } t  j d d … ot t t  j d ƒ } t  j d d … oI d } x/ t  j d d … D] } d | t | ƒ } qŒ Wt j | ƒ q¿ qÃ n t |  | d ƒ } t | ƒ d S(   uÂ   Main 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.

    i   i   i   Ni   i   iÍ (   u   sysu   argvu   intu   hashu   randomu   seedu   rangeu   tabulate(   u   k1u   k2u   xu   au   r(    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu   mainŽ   s    	 u   __main__(   u   __doc__u   sysu   timeu   randomu   marshalu   tempfileu   osu
   gettempdiru   tdu
   randfloatsu   flushu   doitu   tabulateu   mainu   __name__(    (    (    u*   /mit/python/lib/python3.0/test/sortperf.pyu   <module>   s   	'			L	