CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    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

  2. #2
    Join Date
    Aug 2003
    Location
    Greece
    Posts
    206
    Well, the biggest part of you loop can be replaced with an sscanf() and then a "smart" sprintf. There is no need for any memory allocation on the heap (as is done through all these string manipulation methods) since the final string you are trying to build has a finite size (less than 100 bytes).

    Could you please supply some examples of the externally provided strings (those you store in the myStruct structure?

    Regards,

    --harizak

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: please help me improve efficency

    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

  4. #4
    Join Date
    Sep 2003
    Posts
    815
    I will really appreceate to get some suggestion, on how to make this code looks better (I think it's quite a mess at the moment)


    harizak

    The final string I am trying to build hasn't got a finite size (how did you get to a 100), since I don't know in advance the list size
    (note that I add to my string in each iteration)
    can you please be more specific about sscanf() and sprintf()
    thanks



    Paul

    the code is working it's just look kind of ugly (at least to my opinion), but it's working, about the efficiency, that's another problem

    thanks
    Last edited by avi123; October 14th, 2003 at 01:50 AM.

  5. #5
    Join Date
    Sep 2003
    Posts
    815
    Paul McKenzie

    can you recommand me of a code profiler ?
    Is it free?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured