Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everybody.

I am working on a performance critical project which requires a lot of char string handling and char string manipulation.

Using the std::string for the mentioned purposes, is slowing down the application and I am in desperate need of something faster.

What would be the fastest approach to handle the char strings?
Any help would be highly appreciated.

Thanks in advance.
Posted
Updated 1-Feb-11 4:14am
v2

Well you need to define what you mean by "string handling" and "string manipulation". By manipulation are you talking about string copying, concatenation or what?

If you have a need for string concatenation, look for this SO discussion[^], for string searching, see 'fast string search'[^]. By no means this is the end game, there is a whole lot more, but it will be decent starting point.
 
Share this answer
 
Comments
Pallab_GT 1-Feb-11 10:35am    
string handling and string manipulation involves splitting the string upon a character,converting the string to binary,concatenating,replacing character,converting to hex digits etc
Sergey Alexandrovich Kryukov 1-Feb-11 12:39pm    
Good point about clarification. Performance matters needs to considering every technique separately.
5 just for pointing it out.
--SA
Pallab_GT 1-Feb-11 22:40pm    
I sincerely thank you for your suggestion.
If you are doing text processing/parsing - you could consider using
GNU Bison[^]
and
flex[^]

Remeber that you can use flex on its own, and it is reasonably fast :)

Apart from that, I think you will find that nothing beats using pointers, and possibly reading files using ReadFile with OS page sized chunks or memory mapped IO.

If you want to continue using std::string; remember to pass by reference, and remember that you have access to the string contents using the const char* string.c_str() const function.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Feb-11 12:40pm    
Overkill suspected :-) 5 anyway.
--SA
Espen Harlinn 1-Feb-11 14:17pm    
Maybe, but from his description, in an other comment, I suspect that flex alone would be an ideal solution ...
Sergey Alexandrovich Kryukov 1-Feb-11 20:07pm    
Maybe, I don't mind. When I said "overkill" I meant that maybe OP over-simplified the issues, because the question is too general, more elementary overview could be useful. I personally find your advice quite useful.
Thank you.
--SA
Espen Harlinn 2-Feb-11 2:44am    
Thank you SAKryukov, a useful general overview of efficient string/text processing would require more than I can find time for at the moment ... suddenly I find myself a bit pressed for time :)
Pallab_GT 1-Feb-11 22:39pm    
I sincerely thank you for your suggestion.
It depends on which operations you do most often on the string.

One problem can be the way std::string is implemented: just try another STL implementation, like stlport[^]
They use reference counted string, thus removing lot of buffer copy.

Another can be the fact you do frequent insertion and deletion in the middle of the strings.
If so, string (that is: a buffer of memory-consecutive characters) is not as efficient.
Try std::rope [^]instead .

A discussion about the difference can be found here[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-11 12:40pm    
Good - a 5.
--SA
Pallab_GT 1-Feb-11 22:38pm    
I sincerely thank you for your suggestion.
For optimal performance, use char[xx] variables and pointers and avoid making many copies of the strings. Concat in place, and split with pointers or array of pointers, nulling out the split characters in place.
 
Share this answer
 
Comments
Pallab_GT 1-Feb-11 22:38pm    
I sincerely thank you for your suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900