String Functions: Primitive Excitement Beyond the C! Share your comment!

I have avoided talking about the Intel Performance Primitives (IPP) string functions for months now. After all, how exciting can string functions be? Unfortunately, they sometimes are more “exciting” than one would like. How many times have you had to roll your own custom functions in a pinch. Let’s face it: the C runtime library contains a limited bag of tricks with functions such as strcmp, strcat, and strlen.  In real life, those are simply not enough.

Speaking with an associate recently, I was reminded of a project I worked on several years ago, a proxy server plug-in. Incoming internet data was scanned for certain content. In some cases, the incoming payload was altered according to a set of rules. I spent weeks writing custom string manipulation functions to perform the task. (Remember, debugging within a proxy server plug-in context is an extremely difficult task.) So there were times that the custom string handling functions added significant debug time.

C Runtime Equivalences

All the C runtime string functions have an IPP equivalent. For example, to determine whether one string is within another, I use strstr. Intel provides ippsFind_8u and ippsFind_16u, which have more flexibility than strstr. The following two code snippets show how both of these functions can be used in similar ways.

// Note that both strings are expected to be null-terminated.
char *pResult = strstr( szSourceData, szReferenceString );

and 

// Note that not requiring null-terminated strings provides for
// greater flexibility.
IppStatus Status;
int nResult;
Status = ippsFind_8u((Ipp8u*)szSourceData, nSourceLength,
(Ipp8u*)szReferenceString, nReferenceLength,
&nResult);

I recommend you take a look at the Intel documentation, which provides a complete treatment for the IPP string functions here.

New Arrows for Your Quiver

It is always exciting to discover when Intel has provided a new tricks, or as I like to think of them, new arrows for your quiver. My attention was first drawn to ippsInsert_8u and ippsRemove_8u. Had I known about these earlier, I would have traded my best baseball cards for them.

Don’t mistake these two functions as trivial, especially if you are a C# programmer with the rich set of .NET string handling methods, in C or C++.  They alone would have saved lots of time designing, coding, debugging, and profiling during my plug-in project.

There is also a very rich set of regular expression functions in IPP. While the .NET regular expression support has been good in the past, I am no longer impressed with it. I prefer these new IPP functions, which seem more like regular expressions on steroids

For an example of what the IPP string functions are like, you can download the Visual Studio program here.

For a comparison, I benchmarked several of the Intel functions alongside the C runtime functions. In all cases where there were equivalent functions, the execution time for 10,000 function calls were nearly identical between the two.

As a result, performance issues won’t top your decision-making for using the Intel string flavors. What will make a difference is whether they can deliver on functionality that the C runtime lacks. That can save you a lot of programming time, and save your company on the bottom line.

Posted on June 17, 2013 by Rick Leinecker, Slashdot Media Contributing Editor