-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Updated lower and upper #2468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated lower and upper #2468
Conversation
return "".join( | ||
chr(ord(char) + 32) if 65 <= ord(char) <= 90 else char for char in word | ||
) | ||
return "".join(chr(ord(char) + 32) if "A" <= char <= "Z" else char for char in word) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure but in my opinion this change causes a decrease of algorithm speed. Is comparing strings is not faster than comparing ints - in ord()
case? @cclauss Your knowledge is need here! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyone up for creating a timeit benchmark?
I test it using def lower_faster(word: str = "HelloWorld I LOVE PYTHON") -> str:
return "".join(
chr(ord(char) + 32) if 'A' <= char <= 'Z' else char for char in word
)
def lower_slower(word: str = "HelloWorld I LOVE PYTHON") -> str:
return "".join(
chr(ord(char) + 32) if 65 <= ord(char) <= 90 else char for char in word
)
def benchmark() -> None:
import timeit
print(timeit.timeit("lower_slower()", setup="from __main__ import lower_slower", number=1000))
print(timeit.timeit("lower_faster()", setup="from __main__ import lower_faster", number=1000))
if __name__ == "__main__":
benchmark() output:
|
Please remove |
like this below ? print(timeit.timeit("lower_slower()", setup="from __main__ import lower_slower")) |
After I removed def lower_faster(word: str = "HelloWorld I LOVE PYTHON") -> str:
return "".join(
chr(ord(char) + 32) if 'A' <= char <= 'Z' else char for char in word
)
def lower_slower(word: str = "HelloWorld I LOVE PYTHON") -> str:
return "".join(
chr(ord(char) + 32) if 65 <= ord(char) <= 90 else char for char in word
)
def benchmark() -> None:
import timeit
print(timeit.timeit("lower_slower()", setup="from __main__ import lower_slower"))
print(timeit.timeit("lower_faster()", setup="from __main__ import lower_faster"))
if __name__ == "__main__":
benchmark() output
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is always nice to have a benchmark to verify performance.
You are my Python teacher. I learn so much from you. Thanks a lot 👍 |
* update lower and upper * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.