Solved

What are the performance impacts to use C++ style basic_string<char> instead of C style char *?

Posted on 2004-04-16
2
230 Views
Last Modified: 2008-03-06
You can talk about performance in term of both memory usage and time efficiency for general operations like concat, cpy, pattern matching, etc.
0
Comment
Question by:ajaxcal
2 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847698
I assume that u have read thru the benefits of std::string over "C style strings" i.e. char *

Regarding the overhead thind, I really do not see any worthless overhead (if any) against the benefits it provides

Since, the std::string is normally implemented as a char * with some extra range/size checks, it is defenitely better than C style char *

But yes, there would be a considerable increase of size of code becuase of the STL includes taking place

Amit

0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 499 total points
ID: 10847731
Yes there is an overhead using the string class.
So if you really know what you do in C, and manage by yourself some of the features of string, you can get a bit of speed.

An example: string internally stores the length of the string.
In C when you do
   char x[LEN];
   strcpy (x, "abcdefghijklm");
   ...
   strcat (x, y);
   strcat (x, z);
The two strcat are costly as 'strcat' has first to find the end of x, before to add y. And it does it again for z.
While in C++, the length is stored somewhere, so "strcat (x,y)" would be equivalent to "strcpy (x+lenx, y)" which is faster.

But if you manage the length by yourself in C, you can achieve the same result, simulating the 'string' behaviour, e.g.
     char x[LEN];
     int len;
   strcpy (x, "abcdefghijklm");
   len x= strlen(x);
   ...
   strcpy (x+len, y);  // strcpy! at the right place
   len += strlen(y);   // calc the length of y only
   strcpy (x+len, z);
   len += strlen(z);

By understanding well the behaviour of the C funtions and pointers, you can get an efficient program.
But the readiness of a 'string' based program is better :)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question