Skip to content

Improve case convertion performance#766

Merged
jph00 merged 1 commit intoAnswerDotAI:mainfrom
renato145:improve-casing
Feb 2, 2026
Merged

Improve case convertion performance#766
jph00 merged 1 commit intoAnswerDotAI:mainfrom
renato145:improve-casing

Conversation

@renato145
Copy link
Contributor

Recently I had to run camel2snake for many json documents and noticed that calling re.sub(re_pat, ...) is slower than calling re_pat.sub(...).

From what I found, calling re.sub have to check if re_pat is a compiled pattern first.

import timeit

setup = """
import re
_camel_re1 = re.compile('(.)([A-Z][a-z]+)')
x = 'camelCaseString'
"""

t1 = timeit.timeit("re.sub(_camel_re1, r'\\1_\\2', x)", setup=setup, number=1_000_000)
t2 = timeit.timeit("_camel_re1.sub(r'\\1_\\2', x)", setup=setup, number=1_000_000)

print(f"re.sub(_camel_re1, ...): {t1:.3f}s")
print(f"_camel_re1.sub(...):   {t2:.3f}s")

Gives:

re.sub(_camel_re1, ...): 1.410s
_camel_re1.sub(...):   0.675s

@jph00 jph00 merged commit 2cbb9fa into AnswerDotAI:main Feb 2, 2026
9 checks passed
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.

2 participants