Chuck Norrris
Chuck Norrris

Reputation: 384

High performance low latency C++ custom string class

My goal is to find the fastest C++ library for casting int to string, vice versa, and parsing.

Anyone that has experimented with performance of C++ will quickly realize that the string class of STL has terrible performance compared to say STL int arithmetic operations.

Some sample benchmarks from my 3.3 GHz Intel, GCC, CentOS 5.5 machine:

memcpy        0.004000 microsec/op
atoi          0.025000 microsec/op
atof          0.133000 microsec/op
strtod        0.133000 microsec/op
atof          0.135108 microsec/op
(char) uchar  0.001801 microsec/op
(char) ushort 0.001801 microsec/op
cache accs    0.010505 microsec/op
maplookup     0.128534 microsec/op
add_int       0.002456 microsec/op

You can quickly see that string operations will become a bottleneck for any high speed messaging applications.

I have located other libs for high performance strings (listed), but I am writing hoping someone has had similar difficulty and has reached some solution, possibly including writing their own string class.

Upvotes: 14

Views: 3350

Answers (6)

Mackie Messer
Mackie Messer

Reputation: 7348

You didn't provide much information about your servers, but have a look at these libraries from AMD and Intel:

AMD String Library

Intel Integrated Performance Primitives

Both use SSE extensions to speed up string operations.

As far as I can see, they have no atoi(), but you could use the libraries to locate the decimals in the input. Given the string location and length it should be trivial to write a conversion using SSE intrinsics.

Upvotes: 5

Chuck Norrris
Chuck Norrris

Reputation: 384

int castString( const char * str )
{
    int val = 0;
    while( *str ) {
        val = val*10 + (*str++ - '0');
    }
    return val;
}

This is very fast

Upvotes: -1

vitaut
vitaut

Reputation: 55524

The authors of Boost Karma library did a comparison of several integer to string conversion methods here. In this post I did a similar comparison, but including the format library. You don't need a custom string class for this, for example, in case of the format library, the output is stored in an internal buffer which you can convert to an std::string or access as a C string or as an array of characters so you can avoid string creation if necessary.

Upvotes: 0

Tristram Gräbener
Tristram Gräbener

Reputation: 9711

You might want to look at http://alexott.blogspot.fr/2010/01/boostspirit2-vs-atoi.html

It might give you a bigger performance boost if you are parsing something more complicated than a string.

But as some comments said, is there really a bottleneck with string manipulations? Can't you avoid them before hand?

Upvotes: 0

TonyK
TonyK

Reputation: 17114

Send everything in ASCII hex, and write the conversion routines in assembly language.

Upvotes: 0

ArtemGr
ArtemGr

Reputation: 12547

I wrote my own string class (gstring). It's header-only and allows me to reuse stack buffers and wrap C strings easily. Integer encoding is included. Integer decoding is a wrapper around strtol.

Allows me to parse strings easily:

uint32_t pos = 0
gstring gs1 = gstr.netstringAt (pos, &pos); // gs1 is a *view* into gstr
gstring gs2 = gstr.netstringAt (pos, &pos);
int int1 = gstr.intAt (pos, &pos); if (gstr[pos] == ',') ++pos;
int int2 = gstr.intAt (pos, &pos); if (gstr[pos] == ',') ++pos;

There's also Str, but its behaviour on a 64-bit platform isn't very clear to me.
There's also FBString. They promise that "folly::to" conversions are fast.

Upvotes: 0

Related Questions