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

Swap rstrip() and lskip() to reduce execution time of strlen() #177

Merged
merged 1 commit into from
Oct 20, 2024

Conversation

isidroas
Copy link
Contributor

I confess that the optimization is minimal. But it is free because it does not complicates the code.

With this change, I measured the time spent on the first call of strlen():

$ python3 -c 'print(" "*4000 + "name=value")' > long_padding.ini
$ gcc examples/ini_dump.c ini.c -o examples/ini_dump
$ ltrace -T -e strlen ./examples/ini_dump long_padding.ini
ini_dump->strlen("name=value\n")                                     = 11 <0.000147>

and without this change (master):

ini_dump->strlen("                                "...)              = 4011 <0.001856>

The execution time was reduced from 1856 ns to 147 ns.

@benhoyt
Copy link
Owner

benhoyt commented Oct 20, 2024

This makes sense to me, thanks!

@benhoyt benhoyt merged commit cd5f939 into benhoyt:master Oct 20, 2024
2 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