tags:

views:

25

answers:

1

How to reset/emptying to a std::wstring?

It seems that my function is making a delay when using these line:

 std::wstring currentUrl; // <--- I declare this as global.
 currentUrl = _bstr_t(url->bstrVal);

Any idea how can I resolve this?

A: 

How did you measure that delay? The only reliable way is through a profiler, and a profiler would also show you how that time was spent.

That said, assigning to a string often (unless the string can reuse its old buffer or small string optimization kicks in) involves deleting the old buffer and allocating a new buffer. And dynamic memory is slow.

I don't know _bstr_t, but since std::wstring does only have assignment operators to assign from another std::wstring and const wchar_t*, I assume this is the latter. If that is the case, the string doesn't know the size of the string it will get assigned, so if the string is big, it might have to incrementally increase its buffer, which again involves allocation and deallocation plus copying characters, so this might be quite expensive.
You could try to use an assign() member function instead of the assignment operator. I think there's an overload of assign() that takes a const wchar_t* and the size of the string, allowing it to know the exact buffer size before-hand.

However, as always with performance problems, you need to measure using a profiler. Guessing will not get you far.

sbi