16401 – [3.4 Regression] ostringstream in gcc 3.4.x very slow for big data (original) (raw)

| Description Jan Starzynski 2004-07-07 08:10:00 UTC Hello, I'm using ostringstream to write big amounts of data (some 10MB). For simplicity the data is written as single bytes. For gcc 3.3.x the time per character needed is almost constant for different amounts of data. But for 3.4.x this time increases dramatically: the time / char - ratio for 10 MB is 8 times that for 1 MB. This performance-decrease does not happen to string-concatenation using +=. Appended is an example program that has a switch to measure the performance of both string and ostringstream. I'm using linux 2.4.20 with SuSE 8.2 on a pentium 4, 512 MB Ram, 2.4 GHz. The gcc is 3.4.1, but the results are similar to 3.4.0. gcc -v tells: configured with: ../gcc-3.4.1/configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --enable-languages=c,c++,f77,objc,java,ada --disable-checking --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit i486-suse-linux Thread-Modell: posix gcc-Version 3.4.1 ///// compile with ///// g++ -O2 -march=pentium4 -Wall -o strstream_t strstream_t.cpp ///// strstream_t.cpp ///// /// 0: use ostringstream /// 1: use string #define STR_ONLY 0 #include #include <sys/time.h> #include <sys/resource.h> #include <unistd.h> #if STR_ONLY # include #else # include #endif using namespace std; static double time2dbl(long sec, long usec) { return sec + usec * 1e-6; } /// gives time in seconds and microseconds static void FineTimeInt(unsigned int *sec, unsigned int *musec) { static struct timeval last_tv; struct timeval tv; gettimeofday(&tv,NULL); last_tv = tv; *sec = tv.tv_sec; *musec = tv.tv_usec; } /// gives time in seconds static double FineTime(void) { unsigned int sec, musec; FineTimeInt(&sec, &musec); return time2dbl(sec, musec); } int main() { double oldr = 1; for(int n = 1; n <= 10000000; n *= 10) { #if STR_ONLY string str; #else ostringstream str; #endif double t0 = FineTime(); for(int i = 0; i < n; i++) { #if STR_ONLY str += char(i); #else str << char(i); #endif } double t1 = FineTime(); double t = t1 - t0; #if STR_ONLY double l = str.length(); #else double l = str.str().length(); #endif double r = t / l; cout << n << " " << l << " " << t << " " << r << " " << r / oldr << endl; oldr = r; } } Comment 1 Paolo Carlini 2004-07-07 09:06:34 UTC Confirmed, sigh. The fix is easy, just grow the internal string object exponentially and not one page at a time: I tried to be too smart during the redesign of sstream... Anyway, I should have a patch shortly. Thanks for your report and sorry. Comment 2 Jan Starzynski 2004-07-07 09:49:41 UTC Subject: Re: ostringstream in gcc 3.4.x very slow for big data > Anyway, I should have a patch shortly. Thanks for your > report and sorry. It's a pitty I did'nt report that earlier as I saw with 3.4.0 and: shit happens ;-) Comment 4 Paolo Carlini 2004-07-07 10:10:47 UTC ;) Anyway, I have attached a patch (already regtested) that you can use in your local tree, before 3.4.2 is out. Of course, in case you encounter any problem please let us know ASAP (this time ;) Comment 7 Paolo Carlini 2004-07-14 16:55:09 UTC Fixed for 3.4.2. | | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |