Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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