Created
January 12, 2022 22:31
-
-
Save jbn/5c443b9701dd664c575c89c25050d78f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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