?
Solved

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

Posted on 2004-04-16
2
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1497 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses

741 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