Solved

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

Posted on 2004-04-16
2
229 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now