Solved

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

Posted on 2004-04-16
2
228 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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

12 Experts available now in Live!

Get 1:1 Help Now