This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Replacing string += "." by '.'
- From: Wolfgang Bangerth <bangerth at ticam dot utexas dot edu>
- To: libstdc++ at gcc dot gnu dot org
- Cc: Paolo Carlini <pcarlini at unitus dot it>
- Date: Mon, 25 Nov 2002 10:32:56 -0600 (CST)
- Subject: Replacing string += "." by '.'
Hi all,
triggered by a recent patch of Paolo, I thought about the implications of
replacing adding single characters like
__str += '=';
by
__str += "=";
It seemed to me that the latter is more expensive. The reasoning is the
following call chain:
string::operator += (_CharT *)
string::append (_CharT *)
This calls
append(__s, traits_type::length(__s))
which basically needs to call strlen on __s.
On the other hand, adding the single character (i.e. '.' instead of ".")
has the following call chain:
string::operator += (_CharT c)
string::append (1,c) // length already known
The rest should have equal costs.
The consequence is to add single characters to strings wherever possible.
The same reasoning also applies when writing
cout << ".";
or
cout << '.';
The appended patch does this throughout the libstdc++ directory (sans
testsuite). There are not so terribly many places where this applies, but
there are several more where operations like
strcmp(__s, "C")
and
strcpy(_M_name_messages, "C");
are performed, usually with the "C" locale. The first could be replaced by
(__s[0] == 'C' && __s[1]=='\0')
and for the second something similar might be possible, removing the loop
that would be necessary for these operations otherwise. I have not done
that in this patch.
I don't know how to test the patch. In case someone would like to consider
it, please test it according to the your principles before applying it.
Thanks
Wolfgang
-------------------------------------------------------------------------
Wolfgang Bangerth email: bangerth@ticam.utexas.edu
www: http://www.ticam.utexas.edu/~bangerth
Index: include/std/std_complex.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/include/std/std_complex.h,v
retrieving revision 1.2
diff -u -3 -r1.2 std_complex.h
--- include/std/std_complex.h 28 Jan 2002 22:13:10 -0000 1.2
+++ include/std/std_complex.h 25 Nov 2002 16:30:10 -0000
@@ -390,7 +390,7 @@
__s.flags(__os.flags());
__s.imbue(__os.getloc());
__s.precision(__os.precision());
- __s << '(' << __x.real() << "," << __x.imag() << ')';
+ __s << '(' << __x.real() << ',' << __x.imag() << ')';
return __os << __s.str();
}
Index: src/locale.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/src/locale.cc,v
retrieving revision 1.71
diff -u -3 -r1.71 locale.cc
--- src/locale.cc 24 Nov 2002 18:48:35 -0000 1.71
+++ src/locale.cc 25 Nov 2002 16:30:10 -0000
@@ -235,14 +235,14 @@
for (size_t __j = 0; __j < __i; ++__j)
{
__str += _S_categories[__j];
- __str += "=";
+ __str += '=';
__str += __res;
- __str += ";";
+ __str += ';';
}
__str += _S_categories[__i];
- __str += "=";
+ __str += '=';
__str += __env;
- __str += ";";
+ __str += ';';
__i++;
for (; __i < _S_categories_size
+ _S_extra_categories_size; ++__i)
@@ -251,9 +251,9 @@
if (!__env || strcmp(__env, "") == 0)
{
__str += _S_categories[__i];
- __str += "=";
+ __str += '=';
__str += __res;
- __str += ";";
+ __str += ';';
}
else if (strcmp(__env, "C") == 0
|| strcmp(__env, "POSIX") == 0)
@@ -264,9 +264,9 @@
else
{
__str += _S_categories[__i];
- __str += "=";
+ __str += '=';
__str += __env;
- __str += ";";
+ __str += ';';
}
}
__str.erase(__str.end() - 1);
@@ -347,15 +347,15 @@
else
{
__ret += _S_categories[0];
- __ret += "=";
+ __ret += '=';
__ret += _M_impl->_M_names[0];
for (size_t __i = 1;
__i < _S_categories_size + _S_extra_categories_size;
++__i)
{
- __ret += ";";
+ __ret += ';';
__ret += _S_categories[__i];
- __ret += "=";
+ __ret += '=';
__ret += _M_impl->_M_names[__i];
}
}