<s> """<STR_LIT>""" <EOL> COMPARED_IMPLEMENTATIONS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> REPEAT_COUNT = <NUM_LIT:5> <EOL> import re <EOL> import sys <EOL> import timeit <EOL> from time import time <EOL> from itertools import combinations <EOL> from functools import partial <EOL> try : <EOL> from pyximport . pyxbuild import pyx_to_dll <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> so_path = pyx_to_dll ( '<STR_LIT>' ) <EOL> import os . path <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( so_path ) ) <EOL> from acora import AcoraBuilder , BytesAcora , UnicodeAcora , PyAcora <EOL> def prepare_benchmark_data ( ) : <EOL> s = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> s = s . lower ( ) + s + s . upper ( ) <EOL> search_string = s * <NUM_LIT:1000> <EOL> all_keywords = [ <EOL> '<STR_LIT>' , '<STR_LIT:abc>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> all_keywords = list ( map ( unicode , all_keywords ) ) <EOL> search_string = unicode ( search_string ) <EOL> return search_string , all_keywords <EOL> def compare_search ( s , filename , ignore_case , * keywords ) : <EOL> setup_pya = setup_cya = setup_re = <NUM_LIT:0> <EOL> run_pa = '<STR_LIT>' in COMPARED_IMPLEMENTATIONS <EOL> run_ca = '<STR_LIT>' in COMPARED_IMPLEMENTATIONS <EOL> run_re = '<STR_LIT>' in COMPARED_IMPLEMENTATIONS <EOL> if run_pa : <EOL> t = time ( ) <EOL> builder = AcoraBuilder ( keywords , ignore_case = ignore_case ) <EOL> py_acora = builder . build ( acora = PyAcora ) <EOL> setup_pya = time ( ) - t <EOL> t = time ( ) <EOL> if run_ca : <EOL> t = time ( ) <EOL> builder = AcoraBuilder ( keywords , ignore_case = ignore_case ) <EOL> c_acora = builder . build ( ) <EOL> setup_ca = time ( ) - t <EOL> if run_re : <EOL> t = time ( ) <EOL> if hasattr ( keywords [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> kw_regexp = '<STR_LIT:|>' . join ( keywords ) <EOL> else : <EOL> kw_regexp = '<STR_LIT:|>' . encode ( '<STR_LIT>' ) . join ( keywords ) <EOL> if ignore_case : <EOL> regexp = re . compile ( kw_regexp , re . I ) <EOL> else : <EOL> regexp = re . compile ( kw_regexp ) <EOL> setup_re = time ( ) - t <EOL> print ( "<STR_LIT>" % ( <EOL> ignore_case and '<STR_LIT>' or '<STR_LIT>' , <EOL> builder . for_unicode and '<STR_LIT>' or '<STR_LIT>' , <EOL> setup_pya , setup_ca , setup_re ) ) <EOL> if run_pa : <EOL> timings = timeit . Timer ( partial ( py_acora . findall , s ) ) . repeat ( number = REPEAT_COUNT ) <EOL> print ( "<STR_LIT>" % min ( timings ) ) <EOL> if run_ca : <EOL> timings = timeit . Timer ( partial ( c_acora . findall , s ) ) . repeat ( number = REPEAT_COUNT ) <EOL> print ( "<STR_LIT>" % min ( timings ) ) <EOL> if filename : <EOL> if run_pa : <EOL> timings = timeit . Timer ( partial ( py_acora . filefindall , filename ) ) . repeat ( number = REPEAT_COUNT ) <EOL> print ( "<STR_LIT>" % min ( timings ) ) <EOL> if run_ca : <EOL> timings = timeit . Timer ( partial ( c_acora . filefindall , filename ) ) . repeat ( number = REPEAT_COUNT ) <EOL> print ( "<STR_LIT>" % min ( timings ) ) <EOL> if run_re : <EOL> timings = timeit . Timer ( partial ( regexp . findall , s ) ) . repeat ( number = REPEAT_COUNT ) <EOL> print ( "<STR_LIT>" % min ( timings ) ) <EOL> return ( <EOL> run_pa and py_acora . findall ( s ) or None , <EOL> run_ca and c_acora . findall ( s ) or None , <EOL> run_pa and ( filename and py_acora . filefindall ( filename ) ) or None , <EOL> run_ca and ( filename and c_acora . filefindall ( filename ) ) or None , <EOL> run_re and regexp . findall ( s ) or None <EOL> ) <EOL> def run_benchmark ( search_string , all_keywords ) : <EOL> search_string_lower = search_string . lower ( ) <EOL> bytes_search_string = search_string . encode ( '<STR_LIT>' ) <EOL> bytes_search_string_lower = search_string_lower . encode ( '<STR_LIT>' ) <EOL> import tempfile <EOL> temp_text_file = tempfile . NamedTemporaryFile ( ) <EOL> temp_text_file . write ( bytes_search_string ) <EOL> temp_text_file . flush ( ) <EOL> filename = temp_text_file . name <EOL> for i in range ( len ( all_keywords ) , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> for keywords in combinations ( all_keywords , i ) : <EOL> print ( '<STR_LIT>' % ( len ( keywords ) , '<STR_LIT:U+0020>' . join ( sorted ( keywords ) ) ) ) <EOL> keywords_lower = [ kw . lower ( ) for kw in keywords ] <EOL> results = compare_search ( search_string , None , False , * keywords ) <EOL> for result in results [ : <NUM_LIT:2> ] : <EOL> assert_equal ( results , result , search_string , keywords ) <EOL> results = compare_search ( search_string , None , True , * keywords ) <EOL> for result in results [ : <NUM_LIT:2> ] : <EOL> assert_equal ( results , result , search_string_lower , keywords_lower ) <EOL> keywords = [ keyword . encode ( '<STR_LIT>' ) for keyword in keywords ] <EOL> results = compare_search ( bytes_search_string , filename , False , * keywords ) <EOL> for result in results [ : <NUM_LIT:4> ] : <EOL> assert_equal ( results , result , bytes_search_string , keywords ) <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> keywords_lower = [ keyword . encode ( '<STR_LIT>' ) for keyword in keywords_lower ] <EOL> results = compare_search ( bytes_search_string , filename , True , * keywords ) <EOL> for result in results [ : <NUM_LIT:4> ] : <EOL> assert_equal ( results , result , bytes_search_string_lower , keywords_lower ) <EOL> def assert_equal ( results , result , search_string , keywords ) : <EOL> if result is None : <EOL> return <EOL> assert len ( result ) == sum ( map ( search_string . count , keywords ) ) , "<STR_LIT>" % ( <EOL> sum ( map ( search_string . count , keywords ) ) , <EOL> len ( result ) , <EOL> [ ( len ( res ) if res is not None else None ) for res in results ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_benchmark ( * prepare_benchmark_data ( ) ) </s>
