please help me improve efficency
Hello,
As you can see from my last threads, I need to some casting on char *,
InOrder to do it I use both std:string and also functions that work on char * (for example atoi)
together it looks like a mess and is not very efficent, so therefore I attach part of my code here, and I'll be happy to get advice of how to improve both efficiency and the way the code looks like (but not on efficency expence...)
Thanks in advance:
please note the following:
1. MyStdList is defined like that:
typedef std::list<myStruct*> MyStdList;
2.and myStruct is defined like that:
struct myStruct
{
int nItemNum;
char* pszItemData;
};
3. FormatMyString function is defined like that:
FormatMyString(string& str,int iStringLen);
and it trims the string and add zeros at the begining if string.length is shorter that iStrLen
4. I can't control pszItemData, I get it from another prcosses which determins how it is composed of (meaning first 5 chars are id for example)
so here is my function:
void BuildStr(MyStdList* pList,string& szOutputStr)
{
string sTmpStr;
string sStrHeader= "There are"
char szSizeOfList[6],szLZeroSizeOfList[6];
//szLZeroSizeOfList is szSizeOfList with leading zeros if
//necessary
itoa((int)pList->size(),szSizeOfList[,10);
sprintf(szLZeroSizeOfList,"%05s",szSizeOfList);
sStrHeader+= szLZeroSizeOfList;
sStrHeader += "items in the list";
for(MyStdList::iterator it = pList->begin(); it != pList->end(); it++)
{
myStruct *pMS = (myStruct*)*it;
string sMyItemData = pMS->pszItemData;
sTmpStr+= "Student ID is:";
string sTmp = sMyItemData.substr(0,5);
//student ID compose of first 5 digits
int iStudentID = atoi(sTmp.c_str());
//need iStudentID for later
FormatMyString(sTmp,5);
sTmpStr+= sTmp + "Student Name is:";
sTmp = sMyItemData.substr(5,30);
sTmpStr+= sTmp + "student age is:";
sTmp = sMyItemData.substr(30,33);
if(iStudentID<10000)
sTmpStr+= "This student started before year 2000";
else
sTmpStr+= "This student started in or after year 2000";
sTmp = sMyItemData.substr(33,15);
if(sTmp[14]>= 0 && sTmp[14] <= 0)
//last char in sTmp is numeric
//means this student is on scholership
{
int iScholershipSum = atoi(sTmp.c_str());
iScholershipSum = iScholershipSum/100;
//convert from cents to $$
char szScholershipSum[15];
itoa(iScholershipSum ,szScholershipSum,10);
sTmpStr+= "This student got scholarship, of";
sTmpStr+= szScholershipSum;
}
else
sTmpStr+= "This student didn't get scholarship";
}//end loop
szOutputStr = sStrHeader + sTmpStr;
}
the function is defined like that:
void BuildStr(MyStdList* pList,string& szOutputStr);
and I call it like that:
string szMyStr;
BuildStr(&List,szMyStr);
Thanks again
avi123
Re: please help me improve efficency
Quote:
Originally posted by avi123
Hello,
As you can see from my last threads, I need to some casting on char *,
InOrder to do it I use both std:string and also functions that work on char * (for example atoi)
together it looks like a mess and is not very efficent, so therefore I attach part of my code here, and I'll be happy to get advice of how to improve both efficiency and the way the code looks like (but not on efficency expence...)
You need a code profiler so you know where the slow down takes place. If you try to optimize "by sight", you will in all likelihood waste your time trying to optimize a piece of code that is already optimal. You need to know *exactly* what needs to be sped up, and only a code profiling tool will give you this information. If you don't know what is inefficent, we don't know either.
Also, the way things should go is that you program for correctness first, and then optimize second. If you try and code for both correctness and optimizations, you'll wind up with code that is optimized but doesn't work correctly. Then you have to code in all the corrections to the optimized code, adding maintenance headaches trying to
a) unoptimize the code so you know where the corrections must be made
b) reoptimizing the code again.
Regards,
Paul McKenzie