Skip to content

ENH: optimize STRING_compare by using memcmp - #4572

Merged
juliantaylor merged 1 commit into
numpy:masterfrom
juliantaylor:string-cmp
Sep 2, 2014
Merged

juliantaylor merged 1 commit into
numpy:masterfrom
juliantaylor:string-cmp

Conversation

@juliantaylor

Copy link
Copy Markdown
Contributor

No description provided.

@juliantaylor

Copy link
Copy Markdown
Contributor Author

@charris back in 2008 (382e672) you changed this function to use a loop instead of strncmp as that does not handle nulls, though is there a reason you did not consider memcmp?
its faster on my glibc 2.19 even for small arrays (4 bytes on amd64)

@juliantaylor

Copy link
Copy Markdown
Contributor Author

maybe because memcmp does not stop on null so short strings with long paddings are slower?
maybe its not such a good idea then

@juliantaylor

Copy link
Copy Markdown
Contributor Author

oh wait this is wrong when comparing strings with garbage behind NULL

edit: no the old function does also not stop, but its probably not worth thinking about what is fastest, should not be an important function

@charris

charris commented Mar 31, 2014

Copy link
Copy Markdown
Member

@juliantaylor I don't remember ;) Some of the gcc library functions were inefficient and slow back in the day, memcpy was notorious that way. The function was probably supposed to bail at the first null, so the function may actually be incorrect as stands, although I think strings are filled out with zeros. Looks like strncmp should work and may be better these days.

@juliantaylor

Copy link
Copy Markdown
Contributor Author

@juliantaylor juliantaylor reopened this May 16, 2014

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me, I think it is correct that we compare all bytes, since only trailing 0-bytes are stripped (as\x00d\x00\x00 is 'as\x00d). Probably doesn't matter, butelsize` actually is int I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, heh, in my mind I combined the old and new usage of i, so nvm.

@juliantaylor

Copy link
Copy Markdown
Contributor Author

I guess this can go in, speeds up at least one usecase and is simple

juliantaylor added a commit that referenced this pull request Sep 2, 2014
ENH: optimize STRING_compare by using memcmp
@juliantaylor
juliantaylor merged commit 0f0575c into numpy:master Sep 2, 2014
@juliantaylor
juliantaylor deleted the string-cmp branch September 2, 2014 21:40
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