B
    ˜íY›  ã               @   sr   d dl Zd dlZddlmZ dgZdddddgd d d ddgdddddgd	d
dfdd„Zdd„ Zdd„ Zdd„ Z	dS )é    Né   )Ú
autotimeitÚbench)éd   )iè  iè  FTZfloat64ÚCc             C   sn  t | ƒt |ƒkrtdƒ‚t | ƒt |ƒkr0tdƒ‚tdƒ tdtjtjf ƒ tdƒ tdt|ƒ ƒ tdƒ dg}x2|D ]*}|r’| d	 d
¡¡ qx| d d
¡¡ qxW td 	|¡ƒ dd„ | D ƒ}dg| }td 	|¡ƒ dd„ |D ƒ}dg| }td 	|¡ƒ t
| |||||ƒ}xV|D ]N}	|	d  d¡}
|
d dt | ƒd   }t|	d |	d ƒ}t|t|ƒ ƒ qW dS )aö  
    Bottleneck benchmark.

    Parameters
    ----------
    shapes : list, optional
        A list of tuple shapes of input arrays to use in the benchmark.
    axes : list, optional
        List of axes along which to perform the calculations that are being
        benchmarked.
    nans : list, optional
        A list of the bools (True or False), one for each tuple in the
        `shapes` list, that tells whether the input arrays should be randomly
        filled with one-fifth NaNs.
    dtype : str, optional
        Data type string such as 'float64', which is the default.
    order : {'C', 'F'}, optional
        Whether to store multidimensional data in C- or Fortran-contiguous
        (row- or column-wise) order in memory.
    functions : {list, None}, optional
        A list of strings specifying which functions to include in the
        benchmark. By default (None) all functions are included in the
        benchmark.

    Returns
    -------
    A benchmark report is printed to stdout.

    z-`shapes` and `nans` must have the same lengthz-`shapes` and `axes` must have the same lengthz Bottleneck performance benchmarkz    Bottleneck %s; Numpy %sz2    Speed is NumPy time divided by Bottleneck timez,    NaN means approx one-fifth NaNs; %s usedÚ z           ZNaNé   zno NaNc             S   s&   g | ]}d   t|ƒ d¡¡ d¡‘qS )r   ú r   )ÚjoinÚstrÚsplitÚcenter)Ú.0Úshape© r   ú9lib/python3.7/site-packages/bottleneck/benchmark/bench.pyú
<listcomp>A   s    zbench.<locals>.<listcomp>z            c             S   s*   g | ]"}d   dt|ƒ  d¡¡ d¡‘qS )r   zaxis=r	   r   )r
   r   r   r   )r   Úaxisr   r   r   r   D   s   Únameé   z%7.1fz%11.1fr   Ú
statementsÚsetupsN)ÚlenÚ
ValueErrorÚprintÚbnÚ__version__Únpr   Úappendr   r
   Ú
benchsuiteÚljustÚtimerÚtuple)ÚshapesÚaxesÚnansÚdtypeÚorderÚ	functionsÚheaderÚnanÚsuiteZtestr   ZfmtÚspeedr   r   r   r   	   s8    %



c          
   C   sl   g }t | ƒdkrtdƒ‚xN|D ]F}tjdd" t| d |ƒ}t| d |ƒ}W d Q R X | || ¡ qW |S )Né   zTwo statements needed.Úignore)Zinvalidr   r   )r   r   r   Zerrstater   r   )r   r   r,   ÚsetupZt0Zt1r   r   r   r!   Q   s    
r!   c             C   sb   t jt  | ¡|d}|r8t|jjt jƒr8t j|d d d…< t j 	| ¡}| 
|¡ t j|j| Ž |dS )N)r&   é   )r'   )r   ZarangeZprodÚ
issubclassr&   ÚtypeZinexactr*   ZrandomZRandomStateZshuffleZarrayZreshape)r   r&   r%   r'   ÚaZrsr   r   r   Úgetarray]   s    
r4   c             C   s  g }dd„ }t jddd}|ddg7 }xf|D ]^}	|d k	rB|	|krBq,i }
|	|
d< d	d
g|
d< d|	|	|	|	f }||| ||||ƒ|
d< | |
¡ q,W ddg}xb|D ]Z}	|d k	r²|	|kr²qœi }
|	|
d< ddg|
d< d|	|	f }||| ||||ƒ|
d< | |
¡ qœW ddg}x–|D ]Ž}	|d k	r$|	|kr$qi }
|	|
d< |	dkrHddg|
d< n |	dkr`ddg|
d< ntdƒ‚d|	|	f }||| ||||ƒ|
d< | |
¡ qW t jddd}xj|D ]b}	|d k	rÊ|	|krÊq®i }
|	|
d< ddg|
d< d|	|	f }||| ||||ƒ|
d< | |
¡ q®W |S )Nc             S   st   d}g }xft |||ƒD ]V\}}	}
|t|ƒt|ƒt|
ƒt|ƒt|	ƒ| f }d dd„ | d¡D ƒ¡}| |¡ qW |S )Nz|
        from bottleneck.benchmark.bench import getarray
        a = getarray(%s, '%s', %s, '%s')
        axis=%s
        %sÚ
c             S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r   Úliner   r   r   r   t   s    z1benchsuite.<locals>.getsetups.<locals>.<listcomp>)Úzipr   r
   r   r   )r/   r#   r%   r$   r&   r'   Útemplater   r   r   r*   Úsr   r   r   Ú	getsetupsj   s    zbenchsuite.<locals>.getsetupsÚreduceT)Z	as_stringZrankdataZnanrankdatar   zbn_func(a, axis)zsl_func(a, axis)r   a  
            from bottleneck import %s as bn_func
            try: from numpy import %s as sl_func
            except ImportError: from bottleneck.slow import %s as sl_func
            if "%s" == "median": from bottleneck.slow import median as sl_func
        r   Ú	partitionZargpartitionzbn_func(a, n, axis)zsl_func(a, n, axis)zÞ
            from bottleneck import %s as bn_func
            from bottleneck.slow import %s as sl_func
            if axis is None: n = a.size
            else: n = a.shape[axis] - 1
            n = max(n / 2, 0)
        ÚreplaceÚpushzbn_func(a, nan, 0)zslow_func(a, nan, 0)zbn_func(a, 5, axis)zslow_func(a, 5, axis)zUnknow function namez”
            from numpy import nan
            from bottleneck import %s as bn_func
            from bottleneck.slow import %s as slow_func
        Zmovezbn_func(a, w, 1, axis)zsw_func(a, w, 1, axis)z˜
            from bottleneck.slow.move import %s as sw_func
            from bottleneck import %s as bn_func
            w = a.shape[axis] // 5
        )r   Zget_functionsr   r   )r#   r&   r%   r$   r'   r(   r+   r;   ZfuncsÚfuncÚrunr/   r   r   r   r   f   sh    







r   )
Znumpyr   Z
bottleneckr   r   Ú__all__r   r!   r4   r   r   r   r   r   Ú<module>   s   B	