Skip to content

Instantly share code, notes, and snippets.

@jbn
Created January 12, 2022 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbn/5c443b9701dd664c575c89c25050d78f to your computer and use it in GitHub Desktop.
Save jbn/5c443b9701dd664c575c89c25050d78f to your computer and use it in GitHub Desktop.
from typing import NewType
import timeit
UserID = NewType('UserID', int)
def check_on(user_id: UserID) -> int:
return user_id * 2
def check_on_all_users(n: int):
acc = 0
for i in range(n):
acc += check_on(UserID(i))
return acc
def check_on_all_users_without_cast(n: int):
acc = 0
for i in range(n):
acc += check_on(i)
return acc
if __name__ == '__main__':
print("Running timeit")
print(timeit.timeit("check_on_all_users(10_000)", globals=locals(), number=1_000))
print(timeit.timeit("check_on_all_users_without_cast(10_000)", globals=locals(), number=1_000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment