StaticString Performance Tests

By | December 27, 2015

There’s a lot of seasoned coders out there who are so stuck in the mindset that C is the fastest language, that they are willing to use antiquated and sometimes dangerous coding practices. I came across this on the Arduino forums recently . . .

I’m using String because its easier to operate, concatenate etc

To which a long time forum users replied

Not if you know how to manipulate strings. It’s a crutch. Everything it does. it does using the wrapped string object. The overhead is NOT worth it.

Well, hang on. Regardless of your knowledge of C strings, it IS easier to read, it’s easier to write and (depending on the implementation) avoids virtually all the pitfalls of C strings. C++ string libraries can be very worthwhile.

I get that the Arduino String library is far from ideal, but what I object to is the mentality that C strings are superior in terms of performance to the C++ contenders. There’s an element of truth to that, but in exchange for marginal performance benefits, C strings offer absolutely no protection at all against most common faults. In fact, functions such as sprintf are prohibited by most high integrity coding standards. I believe using C strings is akin to removing the wind-shield from a convertible car to reduce wind resistance. It might go a little faster, but sooner or later you’ll get hit in the eye by a bug and you’ll be left questioning your ability to make decisions. It’s fast because it skimps on safety!

The StaticString library is a good compromise between C string performance, convenience and safety. StaticString adds very little overhead and, like C strings, StaticStrings do not dynamically allocate; their size is fixed. A StaticString with a maximum length of 100 characters will only ever consume 100 bytes just like an equivalent C-string. Speed-wise, StaticStrings are very fast and in some cases they even outperform C strings (float conversion is much faster due to no scientific notation). Take a squiz at this

StaticString vs C string vs std::string. int to string and string to int performance.

StaticString vs C string vs std::string. int to string and string to int performance.

sprintf & atoi are fastest at converting integers. StaticStrings are 0.6s slower and std::string trails by 5.1s.

StaticString vs C string vs std::string. Float to string and string to float performance.

StaticString vs C string vs std::string. Float to string and string to float performance.

std::string was the surprise winner at converting floats to strings and back. std::string destroyed the competition. StaticString came second by 6.8s and sprintf/atof lost by 12.6s.

StaticString wins at building strings.

StaticString wins at building strings.

StaticString is actually a little faster than sprintf and a LOT faster than std::stringstream when it comes to combining strings, int and floats into one string.

StaticString is definitely a sound alternative to raw C strings. In addition to their low memory footprint and great speed, they offer a very convenient C++ interface with easy operator overloads for most common tasks and they protect against issues that have historically plagued C strings.

 

One thought on “StaticString Performance Tests

  1. Pingback: Embedded String Programming | CamelSoftware

Leave a Reply

Your email address will not be published. Required fields are marked *