-
October 12th, 2003, 04:38 AM
#1
which operation is more efficient?
I have a long string (char *) and I need to parse
I'm going to do the parsing on a std:string
should I do
string sTmp = myCharString;
//my parsing (substr mostly)
or for each parsing operation (have at least 10 of them)
xxx =((string)myCharString).substr(0,4));
-
October 12th, 2003, 05:28 AM
#2
I assume you mean the choices are:
a. determining where each the substring occurs and then extracting it.
b. repeatedly copying the whole string and then removing the unwanted portions.
I suggest "a", less heap activity that way.
-rick
-
October 12th, 2003, 05:40 AM
#3
not exactly
choice b is coping once by:
string sTmp = myCharString;
and then do the parsing (about 10 statments)
-
October 12th, 2003, 05:50 AM
#4
So you're just copying the C string into an STL string so you can use the STL string methods?
How often do you need to copy the string?
If this is inside a loop, I'd look for something else.
char *s = "Some,data,I'd,like,to,parse.";
std::string strSome, strData,strLike;
strSome.assign(s + 0, s + 4);
strData.assign(s + 5, s + 9);
strLine.assign(s + 14, s + 18);
I don't know what kind of parsing you are doing so I'm just using numeric offsets here.
-rick
-
October 12th, 2003, 06:39 AM
#5
I looked at http://www.sgi.com/tech/stl/basic_string.html
it says there that assugn is Synonym for operator=
this is what I'm doing:
option 1:
for(List::iterator it = pList->begin(); it != pList->end(); it++)
{
string sData = (*it)->pszData;//pszData is char *
//manipulation over sData (mostly subStr)
//about 10 statments (in each loop)
}
option 2:
for(List::iterator it = pList->begin(); it != pList->end(); it++)
{
((string) (*it)->pszData).substr(....);
//about 10 times like that in each loop
}
-
October 12th, 2003, 06:58 AM
#6
Yikes! Option 1 - that way you only copy the string once per iteration. The compiler might not be smart enough to do common subexression elimination for option 2.
... or Option 3:
Code:
for(List::iterator it = pList->begin(); it != pList->end(); it++)
{
const char *str = (*it)->pszData;
// perform parsing directly on str, don't use std::string methods.
}
-rick
Last edited by rfmobile; October 12th, 2003 at 07:01 AM.
-
October 14th, 2003, 07:45 AM
#7
rfmobile
about option 3:
how do I do operation like substr?
How do I do +?
Thanks
-
October 14th, 2003, 07:54 AM
#8
Method like substr(int s, int e) accepts indexes but STL string methods like pointers so you can use pointers to first and last character of substring.
Pass the pointers to the assign method on a new std::string.
Suppose you were parsing for a substring between two commas ...
Code:
std::string strParse;
std::string strFound;
const char *s = strParse.begin();
// look for first comma.
while (s != strParse.end() && *s != ',') s++;
const char *e = s;
if (*e == ',') e++;
// look for second comma.
while (e != strParse.end() && *e != ',') e++;
// place text between commas in strFound.
strFound.assign(s, e);
-rick
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|