void main()
{
int i;
DWORD count,stop;
wstring str;
count=GetTickCount();
for (i=0;i<1000000; i++)
{
str.append(L"asdfgasdfgasdfgasdfg");// add some random text
str=L"";
}
stop=GetTickCount()-count;
cout<<"for = "<<stop<<endl;
count=GetTickCount();
for (i=0;i<1000000; i++)
{
str.append(L"asdfgasdfgasdfgasdfg");
str.clear();
}
stop=GetTickCount()-count;
cout<<"for clear() "<<stop<<endl;
}
results for this code
1st run
for = 422
for clear() 3478
2nd run
for = 452
for clear() 3588
3rd run
for = 468
for clear() 3400
It's clear for me that using the =L"" to empty the string is faster.
Why did they made that method, or is it doing something that the =L""; isn't doing?
The results are:
MinGW: Both loops average at 359 ms.
VC++: Both loops average at 453 ms.
EDIT 2: I still wasn't sure the loops were fair, so I swapped them. This time, MinGW gave slightly faster times for clear(). Around 50 ms. VC++ gave better times for operator=(). Around 150 ms.
The s2.append(str) is uselss, and it slows the speed. And it is using a lot of memory too, because you never empty it so it will have 2.000.000 * 20 * 2= 80.000.000 bytes = ~76.29 MB.
But I don't get it how you get those values. this works slower for me. And even with the string already stored in a const wchar_t *, it gives me the same values as I had before changing the code. I really don't get it.