F
FFMG
Hi,
I need the php equivalent of explode in one of my app.
I read a very big file and "explode" each line to fill a structure.
The reading of the file, 19Mb, (I will also need to streamline the way
I read each line I guess), takes about 10 seconds. But when I 'explode'
each line the process takes about 140 seconds.
This is what I have tried so far...
//-----------------------function A--------------------------
std::vector<std::string> explode(
const std::string s,
const std::string separator
)
{
const int iPit = separator.length();
std::vector<std::string> ret;
int iPos = s.find(separator, 0);
int iStart = 0;
while(iPos>-1)
{
if(iPos!=0){
ret.push_back(s.substr(iStart,iPos-iStart));
iStart = (iPos+iPit);
}
iPos = s.find(separator, iStart);
} // end while
// add the last item if need be.
if(iStart != s.length()){
ret.push_back(s.substr(iStart));
}
return ret;
}
//-----------------------function B--------------------------
std::vector<std::string> explode(
const char* s,
const char separator
)
{
std::vector<std::string> ret;
char seps[] = {separator};
char *token = strtok( (char*)s, seps );
while( token != NULL )
{
ret.push_back( token );
token = strtok( NULL, seps );
}
return ret;
}
//--------------------------------------------------------------------
Function B is slightly faster than function A.
How could I speed up my Explode?
Many thanks
FFMG
I need the php equivalent of explode in one of my app.
I read a very big file and "explode" each line to fill a structure.
The reading of the file, 19Mb, (I will also need to streamline the way
I read each line I guess), takes about 10 seconds. But when I 'explode'
each line the process takes about 140 seconds.
This is what I have tried so far...
//-----------------------function A--------------------------
std::vector<std::string> explode(
const std::string s,
const std::string separator
)
{
const int iPit = separator.length();
std::vector<std::string> ret;
int iPos = s.find(separator, 0);
int iStart = 0;
while(iPos>-1)
{
if(iPos!=0){
ret.push_back(s.substr(iStart,iPos-iStart));
iStart = (iPos+iPit);
}
iPos = s.find(separator, iStart);
} // end while
// add the last item if need be.
if(iStart != s.length()){
ret.push_back(s.substr(iStart));
}
return ret;
}
//-----------------------function B--------------------------
std::vector<std::string> explode(
const char* s,
const char separator
)
{
std::vector<std::string> ret;
char seps[] = {separator};
char *token = strtok( (char*)s, seps );
while( token != NULL )
{
ret.push_back( token );
token = strtok( NULL, seps );
}
return ret;
}
//--------------------------------------------------------------------
Function B is slightly faster than function A.
How could I speed up my Explode?
Many thanks
FFMG