开发者

Performance tuning

I have the following code segment . For a vector of size 176000 the loop takes upto 8 minutes to execute . I am not sure what is taking so much time

    XEPComBSTR bstrSetWithIdsAsString; //Wrapper class for BSTR
    std::vector<__int64>::const_iterator it;
    for(it = vecIds.begin();
        it != vecIds.end();
        it++)
    {

                   __int64 i64Id = (*it);
                  __int64 i64OID = XPtFunctions::GetOID(i64Id);  

                  // set ',' between two set members
                  if (it != vecIds.begin())
                        bstrSetWithIdsAsString.Append(XEPComBSTR(L","));
                      wchar_t buf[20];
        _i64tow_s(i64OID, buf, 20, 10);
        bstrSetWithIdsAsString.Append(buf);
    }


__int64 GetOID( cons开发者_C百科t __int64 &i64Id)
{
    __int64 numId = i64Id;
    numId <<= 16;
    numId >>= 16;
    return numId;
}


I think your bottleneck is the Append function. You see, the string has some allocated memory inside, and when you try to append something that won't fit, it reallocates more memory, which takes a lot of time. Try allocating necessary memory once in the beginning. HTH


I am not sure what this is doing: bstrSetWithIdsAsString.Append(buf);

but I guess this is where the slowness is, particularly if it has to work out where the end of the buffer is every time by looking for the first zero byte, and possibly needs to do a lot of reallocating.

Why not use wostringstream?


The only way to find out what takes up all this time is to profile the application. Some editions of Visual Studio come with a fully functional profiler.

Alternatively, simply run the program in the debugger, and break it at random intervals, and note where in the code you are.

But I can see a few potential trouble spots:

  • you perform a lot of string appends. Do they allocate new memory each time? Does your string type allow you to reserve memory in advance, like std::string can do? In general, is the string class efficient?
  • you loop over iterators, and given the horrible Hungarian Notation you appear to use, I am assuming you are working on Windows, probably using MSVC. Some versions of MSVC enable a lot of runtime checking of STL iterators even in release builds, unless you explicitly disable it. VS2005 and 2008 specifically are guilty of this. 2010 only enables this checking in debug mode.
  • and, of course, you are building with optimizations enabled, right?

But I'm just pointing out what seems like it could potentially slow down your code. I have no clue what's actually happening. To be sure, I'd have to profile your code. You can do that. I can't. So do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜