EDN Admin
Well-known member
trying to understand when to use a vector<wstring> and how to return such a vector from a function ...
Have a function that writes 250 text lines to a vector and then returns that vector from the function. Performance is pretty bad. And when I changed from the function returning vector<wstring> to return a unique_ptr<vector<wstring>> I see
no performance difference. I was expecting that returning a unique_ptr<vector<wstring>> would be faster than returning the vector<wstring> because a copy of the vector is not required.
If vector<wstring> is as slow as I am finding it, what to use instead?
Why would a function that returns vector<wstring> perform the sames as one that returns unique_ptr<vector<wstring>>?
thanks,
<pre class="prettyprint void Tester( HWND hWnd )
{
int nbrLines = 250 ;
int nbrIterations = 1000 ;
{
auto startTick = ::GetTickCount( ) ;
for(int cx = 0 ; cx < nbrIterations ; ++cx )
{
auto pLines = GetLinesMethod2(nbrLines) ;
}
auto stopTick = ::GetTickCount( ) ;
int elap = stopTick - startTick ;
wstring msg = L"method 2. elapsed:" + std::to_wstring(elap) ;
::MessageBox(hWnd, msg.c_str( ), L"Elapsed time", MB_OK ) ;
}
{
auto startTick = ::GetTickCount( ) ;
for(int cx = 0 ; cx < nbrIterations ; ++cx )
{
auto lines = GetLinesMethod1(nbrLines) ;
}
auto stopTick = ::GetTickCount( ) ;
int elap = stopTick - startTick ;
wstring msg = L"method 1. elapsed:" + std::to_wstring(elap) ;
::MessageBox(hWnd, msg.c_str( ), L"Elapsed time", MB_OK ) ;
}
}
vector<wstring> GetLinesMethod1(int NbrLines )
{
vector<wstring> lines ;
for( int ix = 0 ; ix < NbrLines ; ++ix )
{
lines.push_back(wstring(L"path is a zero length string. " + ac::to_wstring(ix))) ;
}
return lines ;
}
std::unique_ptr<vector<wstring>> GetLinesMethod2(int NbrLines )
{
std::unique_ptr<vector<wstring>> pLines( new vector<wstring>( )) ;
for( int ix = 0 ; ix < NbrLines ; ++ix )
{
pLines->push_back(wstring(L"path is a zero length string. " + ac::to_wstring(ix))) ;
}
return pLines ;
}
[/code]
<br/>
View the full article
Have a function that writes 250 text lines to a vector and then returns that vector from the function. Performance is pretty bad. And when I changed from the function returning vector<wstring> to return a unique_ptr<vector<wstring>> I see
no performance difference. I was expecting that returning a unique_ptr<vector<wstring>> would be faster than returning the vector<wstring> because a copy of the vector is not required.
If vector<wstring> is as slow as I am finding it, what to use instead?
Why would a function that returns vector<wstring> perform the sames as one that returns unique_ptr<vector<wstring>>?
thanks,
<pre class="prettyprint void Tester( HWND hWnd )
{
int nbrLines = 250 ;
int nbrIterations = 1000 ;
{
auto startTick = ::GetTickCount( ) ;
for(int cx = 0 ; cx < nbrIterations ; ++cx )
{
auto pLines = GetLinesMethod2(nbrLines) ;
}
auto stopTick = ::GetTickCount( ) ;
int elap = stopTick - startTick ;
wstring msg = L"method 2. elapsed:" + std::to_wstring(elap) ;
::MessageBox(hWnd, msg.c_str( ), L"Elapsed time", MB_OK ) ;
}
{
auto startTick = ::GetTickCount( ) ;
for(int cx = 0 ; cx < nbrIterations ; ++cx )
{
auto lines = GetLinesMethod1(nbrLines) ;
}
auto stopTick = ::GetTickCount( ) ;
int elap = stopTick - startTick ;
wstring msg = L"method 1. elapsed:" + std::to_wstring(elap) ;
::MessageBox(hWnd, msg.c_str( ), L"Elapsed time", MB_OK ) ;
}
}
vector<wstring> GetLinesMethod1(int NbrLines )
{
vector<wstring> lines ;
for( int ix = 0 ; ix < NbrLines ; ++ix )
{
lines.push_back(wstring(L"path is a zero length string. " + ac::to_wstring(ix))) ;
}
return lines ;
}
std::unique_ptr<vector<wstring>> GetLinesMethod2(int NbrLines )
{
std::unique_ptr<vector<wstring>> pLines( new vector<wstring>( )) ;
for( int ix = 0 ; ix < NbrLines ; ++ix )
{
pLines->push_back(wstring(L"path is a zero length string. " + ac::to_wstring(ix))) ;
}
return pLines ;
}
[/code]
<br/>
View the full article