Home > C++, VC++ > Optimal swapping of stl containers

Optimal swapping of stl containers

Never use a temporary variable to swap two stl containers instead always use std::swap function. This function has different overloaded versions specialized in swapping every standard container.

So what’s the difference if we use a temporary variable swap and std::swap. The idea is that basically all stl containers use dynamic memory to store data, so for swapping such two containers we don’t have to copy every item in such a container but just to swap few internal pointers, since both containers are of the same type.

Here is a small function and it’s output which swaps two vectors containing plenty of elements. Take a look at the output…

void TestSwap()
    typedef double VT;
    typedef std::vector VTVector;

    // Count of elements
    const VTVector::size_type Size = 1000000;

    // Create two vector objects
    VTVector Vect1;
    VTVector Vect2;

    // Fill both vectors with random elements
    std::generate_n( std::back_inserter( Vect1 ), Size, rand );
    std::generate_n( std::back_inserter( Vect2 ), Size, rand );

    // Now swap and test performance //

// Performance measurement class
    Performance Perf;

// No scientific notation needed and also increase precision
    std::cout < < std::fixed << std::setprecision( 16 ); // Using temporary variable     {Perf.Start();     VTVector VTTemp = Vect1;     Vect1 = Vect2;     Vect2 = VTTemp;     const double PerfVal = Perf.Stop();     std::cout << "Swap performance with temporary variable: " << PerfVal << " ms";} // Using std::swap function     {Perf.Start();     std::swap( Vect1, Vect2 );     const double PerfVal = Perf.Stop();     std::cout << "\nSwap performance with std::swap function: " << PerfVal << " ms\n";} } Output: Swap performance with temporary variable: 0.0191267064952015 ms Swap performance with std::swap function: 0.0000026732372498 ms [/sourcecode] So the output settles all debates. 🙂 Performance measurment class sourcecode is available

  1. October 16, 2009 at 3:50 am

    This depends on which STL you’re using. For example, using STLPort I noticed that in debug std::swap was roughly 5 times as fast, however in release it was 5 times slower.

  2. March 8, 2009 at 11:00 pm

    I would say whether your standard library includes a specialization of std::swap for STL containers or not is a QoI (quality of implementation) issue. You should prefer using the “native” swap function containers offer: Vect1.swap(Vect2).

    Also, you should NOT fully qualify standard functions like swap. Instead of “std::swap(x,y);” consider writing “using std::swap; swap(x,y);”. The difference is that the first version disables ADL (argument dependent lookup). But that’s usually not what you want. You want to select the most specialized swap function (possibly residing in another namespace, thanks to ADL).

    This is covered in “Effective C++” by Scott Meyers (a book worth buying). You might also be interested in Herb Sutter’s article “Namespaces and the Interface Principle”

    – P

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: