Skip to content

Instantly share code, notes, and snippets.

@birkin
Created December 19, 2019 21:28
Show Gist options
  • Select an option

  • Save birkin/d9f948b3a209761b1f6b267ad03a1996 to your computer and use it in GitHub Desktop.

Select an option

Save birkin/d9f948b3a209761b1f6b267ad03a1996 to your computer and use it in GitHub Desktop.
timing an lru_cache decorator.
"""
Run from 'stuff' directory
"""
import timeit
code_setup = '''
import random
from mp_vl_project.mp_vl_app.lib import views_api_entries_helper
'''
cache_code = '''
rand_int = random.randint(1,12)
views_api_entries_helper.stringify_month( rand_int )
'''
non_cache_code = '''
rand_int = random.randint(1,12)
views_api_entries_helper.stringify_month_nocache( rand_int )
'''
cache_code_raw_rslt = timeit.repeat( setup=code_setup, stmt=cache_code, number=100000, repeat=3 )
print( f'cache_code_raw_rslt, ```{cache_code_raw_rslt}```' )
non_cache_code_raw_rslt = timeit.repeat( setup=code_setup, stmt=non_cache_code, number=100000, repeat=3 )
print( f'non_cache_code_raw_rslt, ```{non_cache_code_raw_rslt}```' )
cache_rslt = sorted( cache_code_raw_rslt )[1]
print( f'cache_rslt, ```{cache_rslt}```' )
non_cache_rslt = sorted( non_cache_code_raw_rslt )[1]
print( f'non_cache_rslt, ```{non_cache_rslt}```' )
times_faster = non_cache_rslt / cache_rslt
print( f'cache_rslt is `{times_faster}` times faster' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment