Skip to content

Instantly share code, notes, and snippets.

@LucasMagnum
Created April 3, 2020 20:59
Show Gist options
  • Save LucasMagnum/503b615daa6b2ca8dfba94d6dbcbde4d to your computer and use it in GitHub Desktop.
Save LucasMagnum/503b615daa6b2ca8dfba94d6dbcbde4d to your computer and use it in GitHub Desktop.
import functools
import random
import time
import sys
def timeit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
response = func(*args, **kwargs)
took = time.time() - start
func_name = func.__name__
print(f"{func_name} --> took {took:.2f} ")
return response
return wrapper
@timeit
def list_solution(values, collection):
for value in values:
assert value in collection
size = 30_000_000
collection = [x for x in range(size)]
collection_size_mb = sys.getsizeof(collection) / 1024 / 1024
print(f"Collection size: {collection_size_mb:.2f}mb")
values = [random.randint(0, size) for _ in range(100)]
list_solution(values, collection)
import functools
import random
import time
import sys
def timeit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
response = func(*args, **kwargs)
took = time.time() - start
func_name = func.__name__
print(f"{func_name} --> took {took:.2f} ")
return response
return wrapper
@timeit
def tuple_solution(values, collection):
for value in values:
assert value in collection
size = 30_000_000
collection = tuple(x for x in range(size))
collection_size_mb = sys.getsizeof(collection) / 1024 / 1024
print(f"Collection size: {collection_size_mb:.2f}mb")
values = [random.randint(0, size) for _ in range(100)]
tuple_solution(values, collection)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment