Skip to content
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

Perf: Replace is_iterable function + use mapped pre-compiled Patterns #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PaarthShah
Copy link

No description provided.

Comment on lines -56 to -61
def get_underscoreize_re(options):
if options.get("no_underscore_before_number"):
pattern = r"([a-z0-9]|[A-Z]?(?=[A-Z](?=[a-z])))([A-Z])"
else:
pattern = r"([a-z0-9]|[A-Z]?(?=[A-Z0-9](?=[a-z0-9]|(?<![A-Z])$)))([A-Z]|(?<=[a-z])[0-9](?=[0-9A-Z]|$)|(?<=[A-Z])[0-9](?=[0-9]|$))"
return re.compile(pattern)
Copy link
Author

@PaarthShah PaarthShah May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About a 10% improvement for the first pattern, and 27% for the second, at least on my machine.
The change I made allows the pattern to be compiled exactly once on module instantiation, rather than repeatedly every time this function is entered.

Before:

from djangorestframework_camel_case.util import camel_to_underscore
timeit.timeit(lambda: camel_to_underscore("someName"))
2.591615671000909
timeit.timeit(lambda: camel_to_underscore("someName5", some_number_before_number=True))
2.839183228003094

After:

import timeit
from djangorestframework_camel_case.util import camel_to_underscore
timeit.timeit(lambda: camel_to_underscore("someName"))
2.3492436910019023
timeit.timeit(lambda: camel_to_underscore("someName5", some_number_before_number=True))
2.227818359999219

@PaarthShah PaarthShah changed the title Replace is_iterable function + use mapped pre-compiled Patterns Perf: Replace is_iterable function + use mapped pre-compiled Patterns May 19, 2024
@PaarthShah
Copy link
Author

@vbabiy Any objections to this?

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.

1 participant