Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created August 1, 2021 02:18
Show Gist options
  • Save joncloud/1c24df8f41275b06b26b816293e7bc9a to your computer and use it in GitHub Desktop.
Save joncloud/1c24df8f41275b06b26b816293e7bc9a to your computer and use it in GitHub Desktop.
Quick Performance Test
import typing
from datetime import datetime
import pandas as pd
def create_timed_function(fn: typing.Callable[[], None]):
def timed_function():
start = datetime.now()
fn()
end = datetime.now()
print(f'{fn.__name__}: {(end - start).total_seconds():.2f}s')
return timed_function
BASE_TOTAL = 5000
def get_base_numbers():
total = BASE_TOTAL
for i in range(total):
yield i
def get_test_numbers():
total = BASE_TOTAL + 100
for i in range(total):
yield i - 50
def test_list():
l = list(get_base_numbers())
def test_list_main():
yes = False
for num in get_test_numbers():
yes = num in l
return yes
timed_main = create_timed_function(test_list_main)
timed_main()
def test_set():
s = set(get_base_numbers())
def test_set_main():
yes = False
for num in get_test_numbers():
yes = num in s
return yes
timed_main = create_timed_function(test_set_main)
timed_main()
def test_data_frame():
p = pd.DataFrame({
'number': list(get_base_numbers())
})
def test_data_frame():
yes = False
for num in get_test_numbers():
yes = p.number.isin([num]).any()
return yes
timed_main = create_timed_function(test_data_frame)
timed_main()
test_list()
test_set()
test_data_frame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment