Skip to content

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

Merged
merged 2 commits into from
Sep 25, 2020
Merged

Updated lower and upper #2468

merged 2 commits into from
Sep 25, 2020

Conversation

realDuYuanChao
Copy link
Member

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Uncyclopedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

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)
Copy link
Member

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! :)

Copy link
Member

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?

@realDuYuanChao
Copy link
Member Author

realDuYuanChao commented Sep 25, 2020

I test it using timeit. It prove new version is faster.

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:

0.005637370000000003
0.005095732000000002

@cclauss
Copy link
Member

cclauss commented Sep 25, 2020

Please remove number=1000 and re-benchmark.

@realDuYuanChao
Copy link
Member Author

Please remove number=1000 and re-benchmark.

like this below ?

print(timeit.timeit("lower_slower()", setup="from __main__ import lower_slower"))

@realDuYuanChao
Copy link
Member Author

realDuYuanChao commented Sep 25, 2020

After I removed number=1000.

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

4.604586689
4.297654392999999

Copy link
Member

@cclauss cclauss left a 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.

@realDuYuanChao
Copy link
Member Author

You are my Python teacher. I learn so much from you. Thanks a lot 👍

@realDuYuanChao realDuYuanChao merged commit 72fe611 into TheAlgorithms:master Sep 25, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* update lower and upper

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants