This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[Fwd: basic_string<> - useless because of its poor performance]


 


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);
}





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]