- To: Benjamin Kosnik <bkoz at redhat dot com>
- Subject: Re: basic_string<> - useless because of its poor performance
- From: Ryszard Kabatek <Ryszard dot Kabatek at softax dot pl>
- Date: Tue, 03 Jul 2001 09:42:05 +0200
- References: <Pine.SOL.3.91.1010702120808.3621A-100000@taarna.cygnus.com>
- Reply-To: Ryszard dot Kabatek at softax dot pl
Benjamin Kosnik wrote:
>
> Ryszard, you'll need to post sample code before Nathan or others will be
> able to help you in any kind of constructive manner. Would you be willing
> to do this?
I posted a sample some time ago.
OK, let as construct an empty string and then append to it N characters/one character strings
in terms of one. Here are the results (g++ -O2) for the attached benchmark on PIII/750, linux:
1. gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
Execution time of 10000 string::append(char) calls: 0 sec.
Execution time of 10000 string::append(const string&) calls: 0 sec.
Execution time of 100000 string::append(char) calls: 0.02 sec.
Execution time of 100000 string::append(const string&) calls: 0.03 sec.
Execution time of 1000000 string::append(char) calls: 0.18 sec.
Execution time of 1000000 string::append(const string&) calls: 0.26 sec.
2. gcc version 3.0 (Thread model: single)
Execution time of 10000 string::append(char) calls: 0.1 sec.
Execution time of 10000 string::append(const string&) calls: 0.09 sec.
Execution time of 100000 string::append(char) calls: 11.67 sec.
Execution time of 100000 string::append(const string&) calls: 12.23 sec.
Execution time of 1000000 string::append(char) calls: 5931.55 sec.
Execution time of 1000000 string::append(const string&) calls: 6087.62 sec.
--
Ryszard Kabatek
Tel. (Softax) +48 (22) 517 38 31
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
void
test_append_char(int how_much)
{
string buf; // no preallocation
for (int i = 0; i < how_much; ++i)
buf.append(static_cast<string::size_type>(1) , 'x');
}
void
test_append_string(int how_much)
{
string s(static_cast<string::size_type>(1) , 'x');
string buf; // no preallocation
for (int i = 0; i < how_much; ++i)
buf.append(s);
}
void
run_benchmark1(int how_much)
{
clock_t t0 = clock();
test_append_char(how_much);
clock_t t1 = clock();
cout << "Execution time of " << how_much
<< " string::append(char) calls: "
<< (static_cast<float>(t1 - t0)/CLOCKS_PER_SEC) << " sec."<< endl;
}
void
run_benchmark2(int how_much)
{
clock_t t0 = clock();
test_append_string(how_much);
clock_t t1 = clock();
cout << "Execution time of " << how_much
<< " string::append(const string&) calls: "
<< (static_cast<float>(t1 - t0)/CLOCKS_PER_SEC) << " sec." << endl;
}
int main()
{
run_benchmark1(10000);
run_benchmark2(10000);
run_benchmark1(100000);
run_benchmark2(100000);
run_benchmark1(1000000);
run_benchmark2(1000000);
}