swap (a, b) suggestion
Gabriel Dos Reis
Gabriel.Dos-Reis@dptmaths.ens-cachan.fr
Sat Jan 3 17:56:00 GMT 1998
>>>>> ëEricû, Eric Buddington <eric@sparrow.vgernet.net> wrote:
Eric> Greetings.
Eric> I am just learning about STL, and have found a possible improvement
Eric> for the swap() template in include/g++/stl_algobase.h. The template
Eric> here is straightforward:
Eric> template <class T>
Eric> inline void swap(T& a, T& b) {
Eric> T tmp = a;
Eric> a = b;
Eric> b = tmp;
Eric> }
Eric> For integer values, the following works, and is significantly faster:
Eric> template <class T>
Eric> inline void swap(T& a, T& b) {
Eric> a ^= b;
Eric> b ^= a;
Eric> a ^= b;
Eric> }
This works fine for integral types. But about float/double and
more generaly non-integral types (complex<>, ...)
Eric> In my own code, this change sped up a quicksort significantly.
Eric> Explicit versions for char, short, int, and long (and their unsigned
Eric> counterparts) alongside the template would do the trick.
Eric> I'll be interested to hear any comments. Thanks for your collective
Eric> work - egcs has really saved my butt development-wise.
specialisations for the template function swap<>() for
integral types may be added. Uli ?
-- Gaby
More information about the Gcc
mailing list