8000 ENH: optimize STRING_compare by using memcmp by juliantaylor · Pull Request #4572 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: optimize STRING_compare by using memcmp #4572

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 1 commit into from
Sep 2, 2014

Conversation

juliantaylor
Copy link
Contributor

No description provided.

@juliantaylor
Copy link
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)

8000

@juliantaylor
Copy link
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
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
Copy link
Member
charris commented Mar 31, 2014

@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
Contributor Author

turns out this is actually useful when using a void type to sort an array by byte values
http://stackoverflow.com/a/22750502/1633169

@juliantaylor juliantaylor reopened this May 16, 2014
@@ -2598,12 +2599,14 @@ STRING_compare(char *ip1, char *ip2, PyArrayObject *ap)
const unsigned char *c1 = (unsigned char *)ip1;
const unsigned char *c2 = (unsigned char *)ip2;
const size_t len = PyArray_DESCR(ap)->elsize;
Copy link
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
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
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
0