Re: CString -vs- std:string ?
Not sure if someone find this interesting but I recently performed some performance tests for string concatenation on VS2005 Beta.
In outer_f() I called inner_f() outer_loop number of times.
In inner_f() I performed a concatenation inner_loop number of times.
The string that I concated was 20 characters long.
On my machine for:
outer_loop = 1000
inner_loop = 1000
std::string : 0.224415
std::vector : 0.275405
CString : 0.161216
For:
outer_loop = 100000
inner_loop = 10
std::string : 1.77802
std::vector : 3.74253
CString : 1.65125
For:
outer_loop = 10
inner_loop = 100000
std::string : 0.216147
std::vector : 0.220373
CString : 10.565
This was interesting for me:
1. I expected std::vector to perform better.
2. When using a large inner loop (many concatenations on the same string) the performance for CString drops dramatically. Cache issue?
Not claiming that my tests proofs anything particular. I just thought it was interesting to note how different they behaved.
Hope this was of some interest to someone
Re: CString -vs- std:string ?
I compiled the test1.cpp file that Paul attached to his post. I'm running VC++ 7.1 on Windows 2000 Professional, SP4. I'm running a Pentium 4 3.4 GHz with 1GB of RAM.
Code:
Time duration is as follow:
CString = 1156
std::string = 1156
Edit: I ran the test a couple more times.
Code:
Time duration is as follow:
CString = 1141
std::string = 1109
Time duration is as follow:
CString = 1141
std::string = 1093
Time duration is as follow:
CString = 1172
std::string = 1140
Time duration is as follow:
CString = 1157
std::string = 1140
Looks like they're pretty close to me. std::string looks to be a bit ahead, but with a synthetic benchmark like this, that's too close to call.
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
I compiled as a Win32 console program, using MFC in a shared DLL
I just compiled the program on a compiler at work (VC++ 6.0).
And ran the code on the same computer (Windows 2000 Professional).
I got the following results:
Time duration is as follow:
CString = 9156
std::string = 16125
Press any key to continue . .
CString still about twice as fast.
I've also had other experts from experts exchange peform the same test, and they've all come back with results that very from 40% to 400% increase CString peformance over std::string.
I have to conclude that either there's something wrong with your compiler, or you're not compiling the exact same code.
You may also have some third party STL libraries loaded on your computer that is causing the difference.
I compiled the code using ONLY VC++ 6.0 libraries.
I've also made similar test about three years ago on two different client work sites.
They all show similar results with VC++6.0 std::string having significant peformance problems compared to CString.
Quote:
Originally Posted by JohnW@Wessex
Ran the original program on my machine; Athlon 1.4GHz
VC 6.0, Release code, code generation for Pentium.
Time duration is as follow:
LenTime1 = 240 <<<< CString
LenTime2 = 160 <<<< std::string :confused:
LenTime3 = 150
LenTime4 = 181
LenTime5 = 220
Press any key to continue . . .
That looks like the results of running a debug version instead of a release compiled version.
Try running the last code I posted, that has a debug compile directive, that makes sure you're getting results for release version.
Re: CString -vs- std:string ?
Quote:
Originally Posted by Axter
I just compiled the program on a compiler at work (VC++ 6.0).
I have to conclude that either there's something wrong with your compiler, or you're not compiling the exact same code.
How do you explain the others here who are having similar results to mine? They took the code I posted, compiled it, and saw no increase in the speed. I even took your executable you sent, and it showed minimial difference. Therefore the conclusion is not that I or others are doing something wrong.
Quote:
You may also have some third party STL libraries loaded on your computer that is causing the difference.
I compiled the code using ONLY VC++ 6.0 libraries.
I've also made similar test about three years ago on two different client work sites.
I have no other STL library running. It is VC 6.0 and that's it. For such a significant difference as you've stated, again, it could be relatively easy to explain if the code were examined between CString and std::string.
Quote:
They all show similar results with VC++6.0 std::string having significant peformance problems compared to CString.
And there is no signigicant performance problem when myself, Bob Davis, or JohnW@Wessex ran the program, and they, including myself, are the only ones who have tested your code.
Regards,
Paul McKenzie
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
How do you explain the others here who are having similar results to mine? They took the code I posted, compiled it, and saw no increase in the speed. I even took your executable you sent, and it showed minimial difference. Therefore the conclusion is not that I or others are doing something wrong.
I have no other STL library running. It is VC 6.0 and that's it. For such a significant difference as you've stated, again, it could be relatively easy to explain if the code were examined between CString and std::string.
And there is no signigicant performance problem when myself, Bob Davis, or JohnW@Wessex ran the program, and they, including myself, are the only ones who have tested your code.
Regards,
Paul McKenzie
They're not running the last code I posted, which insures the results are release mdoe code.
So untill I see the results of that code, I can't really consider they're post.
Re: CString -vs- std:string ?
At some stage, we can't keep checking for gremlins on our computers and force a "slow" std::string.
Did you take the code that I posted (test1.cpp) and run it? It seems that you did, but are still not in line with Bob's and mine. You can't just keep discounting everything as "user error".
Regards,
Paul McKenzie
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
At some stage, we can't keep checking for gremlins on our computers and force a "slow" std::string.
Did you take the code that I posted (test1.cpp) and run it? It seems that you did, but are still not in line with Bob's and mine. You can't just keep discounting everything as "user error".
Regards,
Paul McKenzie
I haven't try using your code, because the code I've seen you post is not setup right.
The fact that you're asking me this question, makes me think you're not using the right code.
And I'm not just going by my results. I'm also going by results of several other experts from the Experts-Exchange.
And I'm not just going by the results of one computer. I've compiled similar test to this on several different development computers from different clients and ran it in many different machines.
Re: CString -vs- std:string ?
What is not set up right? Take the code, and run it. See what results you get.
I have taken your code, and again, I have posted those results. In no way was I getting 2, 3, 4, or more fold increase in std::string (and I've tried this on two computers, a Pentium 3 running W2K, and XP running on a Pentium 4)
Regards,
Paul McKenzie
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
What is not set up right? Take the code, and run it. See what results you get.
I have taken your code, and again, I have posted those results. In no way was I getting 2, 3, 4, or more fold increase in std::string (and I've tried this on two computers, a Pentium 3 running W2K, and XP running on a Pentium 4)
Regards,
Paul McKenzie
Well, then that's why we're getting different results, which is what I suspected.
I'm not sure what code of yours you're talking about, so please post what code you think is valid again, and if it is valid, I'll run it.
Why is it that you can't use the following code, which is a valid test with correct parameters:
Code:
int dummy_var = 0;
const int SizeData = 512;
void function1(const char*Data)
{
CString strData = Data;
strData.GetBuffer (1000000);
strData.ReleaseBuffer();
for ( int i = 0; i < 1000; ++i)
strData += strData.Mid(0, 500);
dummy_var += strData[99];
}
void function2(const char*Data)
{
std::string strData = Data;
strData.reserve(1000000);
for ( int i = 0; i < 1000; ++i)
strData += strData.substr(0, 500);
dummy_var += strData[99];
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
const QtyTimesTest = 999;
DWORD StartTime, LenTime1=0, LenTime2=0;
char DummyData[SizeData+1];
memset(DummyData, 'x', SizeData);
DummyData[SizeData] = 0;
{
StartTime = GetTickCount();
for (int i = 0;i < QtyTimesTest;++i)
{
function1(DummyData);
}
LenTime1 += (GetTickCount() - StartTime);
}
{
StartTime = GetTickCount();
for (int i = 0;i < QtyTimesTest;++i)
{
function2(DummyData);
}
LenTime2 += (GetTickCount() - StartTime);
}
{
StartTime = GetTickCount();
for (int i = 0;i < QtyTimesTest;++i)
{
function2(DummyData);
}
LenTime2 += (GetTickCount() - StartTime);
}
{
StartTime = GetTickCount();
for (int i = 0;i < QtyTimesTest;++i)
{
function1(DummyData);
}
LenTime1 += (GetTickCount() - StartTime);
}
#ifdef _DEBUG
printf("You're not running release version!!!\nTest is only valid in release version.\n");
#else
printf("Time duration is as follow:\n\
CString = %li\n\
std::string = %li\n",
LenTime1, LenTime2);
#endif
if (!dummy_var) printf("dummy %i", dummy_var);
system("pause");
return nRetCode;
}
Re: CString -vs- std:string ?
See post #28. It is attached.
Regards,
Paul McKenzie
Re: CString -vs- std:string ?
Copying and pasting your exact code, adding the <afxtempl.h> and <string> headers.
Quote:
Time duration is as follow:
CString = 2859
std::string = 2750
Press any key to continue . . .
And my "include directories" when building are as follows:
Quote:
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE
So there are no hidden "STL" implementations being used (or even installed)
Regards,
Paul McKenzie
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
See post #28. It is attached.
Regards,
Paul McKenzie
I didn't even see that attachment before.
I just compiled it and ran it, and I got the the following results:
Time duration is as follow:
CString = 4594
std::string = 8328
Which still shows CString with a 2 to 1 peformance increase.
This is on a work computer Windows 200 Pro ServicePack 4
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
Copying and pasting your exact code, adding the <afxtempl.h> and <string> headers.
And my "include directories" when building are as follows:
So there are no hidden "STL" implementations being used (or even installed)
Regards,
Paul McKenzie
I ran a few times, some times std::string was faster, other times CString was faster, but in both cases, miminal difference. This makes me believe there is a cache issue involved here.
Regards,
Paul McKenzie
Re: CString -vs- std:string ?
Quote:
Originally Posted by Paul McKenzie
Also, I ran your code. There is hardly any difference in the times, even without reserve():
Running VC 6.0, Pentium 4, 3.00 GHZ
Regards,
Paul McKenzie
Running the same tests with the VC 7.1 (with the improved STL code that ships with VC 7.1) this is the output:
Time duration is as follow:
LenTime1 = 94
LenTime2 = 94
LenTime3 = 94
LenTime4 = 93
LenTime5 = 110
Press any key to continue . . .
Re: CString -vs- std:string ?
Quote:
Originally Posted by Axter
I didn't even see that attachment before.
I just compiled it and ran it, and I got the the following results:
Time duration is as follow:
CString = 4594
std::string = 8328
Which still shows CString with a 2 to 1 peformance increase.
This is on a work computer Windows 200 Pro ServicePack 4
Then this needs more investigation, which I really can't do or have time to do at this moment.
As stated in my last post, there are other factors (possibly caching) that are involved other than algorithmic/implementation related. Since others here have run the program and had results similar to mine, this is the only plausible reason (other than not having speed optimization turned on).
It would be good if someone could figure out why there are such vast time differences between what you're getting and others are getting here. Maybe a careful review of the CString/std::string code when concatenations are done may shed some light on this. Who knows, we may need an OS internals expert to pin down what's going on.
Regards,
Paul McKenzie