B
    ǾS                 @   s   d Z ddlmZ ddlZejdd dkrddlZddlZddlZddlm	Z	 dd Z
d	d
 Zejdde
Zejdde
ZejdddeZejdddeZdddZdd ZdddZdd ZdS )z Benchmark against functools.lru_cache.

    Benchmark script from http://bugs.python.org/file28400/lru_cache_bench.py
    with a few modifications.

    Not available for Py < 3.3.
    )print_functionN   )   r   )countc              O   s   d S )N )argskwargsr   r   2lib/python3.7/site-packages/fastcache/benchmark.py_untyped   s    r
   c              O   s   d S )Nr   )r   r   r   r   r	   _typed   s    r   d   )maxsizeT)r   typed   r   c             c   sV   xPt | |D ]B}x<t |D ]0}x*tt |t|dD ]\}}||fV  q4W qW qW d S )N)rangezipr   )minmaxrepeatirjkr   r   r	   _arg_gen   s    r   c                sD   t d t ddddd  fdd}|dd	d
 |ddd d S )N z{:9s} {:>6s} {:>6s} {:>6s}r   Zmeanr   c                sT   dd t  |d d  |d d D }td| t|t|t| t| d S )Nc             S   s    g | ]\}}|d  |d   qS )r   r   ).0pycr   r   r	   
<listcomp>)   s    z7_print_speedup.<locals>.print_stats.<locals>.<listcomp>   z{:9s} {:6.3f} {:6.3f} {:6.3f})r   printformatr   sumlenr   )nameZoff0Zoff1Zarr)resultsr   r	   print_stats(   s    z#_print_speedup.<locals>.print_statsZuntypedr   r   r   r   r   )r!   r"   )r&   r'   r   )r&   r	   _print_speedup%   s
    r(   Fc             C   s   |rt ddd npt d| d d dd | d d | d d  d	d
 t d| d d dd | d d | d d   d S )Nz{:29s} {:>8s}zfunction callzspeed upz{:32s} {:5.2f}r   r   _r   z, )endr   r   )r!   r"   split)Zresinitr   r   r	   _print_single_speedup2   s     r-   c           	   C   s6  t ddd t d t d t ddd tdd g } d	d
ddddg}xd|D ]\}xFdD ]>}d||f }ttjd|d| ddd}| ||g qVW t| dd   qLW t|  t ddd t d t ddd d}g } xBdD ]:}d| }ttjd| ||ddd}| ||g qW tdd t|  d S )NzTest Suite 1 : z

)r*   z>Primarily tests cost of function call, hashing and cache hits.zBenchmark script based onz7    http://bugs.python.org/file28400/lru_cache_bench.pyT)r,   r   z	"spam", iz"spam", "spam", iza=iza="spam", b=iza="spam", b="spam", c=i)_py_untyped
_c_untyped	_py_typed_c_typedz%s(%s)zM
                for i in range(100):
                    {}
                z"from fastcache.benchmark import %s
   i  )setupr   Znumberz

Test Suite 2 :z9Tests millions of misses and millions of hits to quantifyz"cache behavior when cache is full.zKfrom fastcache.benchmark import {}
from fastcache.benchmark import _arg_genz%s(i, j, a="spammy")zD
            for i, j in _arg_gen():
                %s
            r   r   )r!   r-   r   timeitr   r"   appendr(   )r&   r   afstr3   r   r   r	   run:   s@    





r;   )r   r   r   )NF)__doc__Z
__future__r   sysversion_info	functoolsZ	fastcacher5   	itertoolsr   r
   r   	lru_cacher.   Z
clru_cacher/   r0   r1   r   r(   r-   r;   r   r   r   r	   <module>   s"   

