SPO600 optimizing GLIBC function “strcspn”

I choose to optimize the string library function strcspn because it looked like it could use some cleaning up at the very least. First to explain what the function does. STRCSPN take two strings and compares the first string against any of the characters in the second string. Ones a matching character is found, it returns the number of characters in the first string before the matched character. For example string one will be “test1” string two is “1”, the function will return 4, as 4 characters come before the first matched character. Now lets take a look at the current implementation of the function.

strcpsnimplemenation.PNG

It’s a little bit confusing but from I understand, it uses a table and tries to find the matching character at which point it starts calculating the number of characters to return. So far I’ve come up with a few different implementations, mostly getting around 1.5 – 2% increased performance. The implementation I am currently testing is very simple :

myStrcSPN

All we are doing here is looping through the first string and using the bit operator ^ XOR to see if any bits match between the two strings, counting our return value up when we don’t find a match. So far with my testing this function works properly and is about 2% in terms of improvement. Looking to see what I can do to it further to increase either performance or memory.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s