Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when there is performance benefit for writing own swap() function / method

Tags:

c++

swap

Suppose I have class like this one:

struct A{
  std::string a;
  std::string b;
  std::string c;
  std::string d;
};

If I use std::swap, it probably will do something like this:

// pseudo-code:
void std::swap(A &a, A &b){
   A tmp = std::move(a);
   a = std::move(b);
   b = std::move(tmp);
}

It will construct "empty" object tmp using default c-tor - generally cheap operation. Then it hopefully move 3 times, except in crazy cases when move decay to copy.

However if i do my own swap:

void swap(A &a, A &b){
   std::swap(a.a, b.a);
   std::swap(a.b, b.b);
   std::swap(a.c, b.c);
   std::swap(a.d, b.d);
}

It definitely will use less memory, but it still need to construct empty std::string - 4 times !!!

I could go wild, and make it with single std::string.

In all cases it does not look like big improvement.

Only proper case I could think of is if default c-tor is ridiculously expensive. Am I correct?

like image 461
Nick Avatar asked Sep 24 '15 09:09

Nick


People also ask

What is the use of swap function?

The swap() function is used to swap two numbers. By using this function, you do not need any third variable to swap two numbers.

How does the swap function work in Python?

A simple swap function in Python is generally considered a function that takes two initialized variables, a = val_a and b = val_b , and returns a result of a = val_b and b = val_a . The function accepts variables of arbitrary types, or more precisely, variables that are assigned with objects of arbitrary types.

Is there inbuilt swap function in C++?

swap() in C++The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables.

How do you write a swap function in C++?

Example 2: Swap Numbers Without Using Temporary Variables Then, we add a and b and store it in a with the code a = a + b . This means a = 5 + 10 . So, a = 15 now. Then we use the code b = a - b .


1 Answers

Well you can't say that you need to, or never should, make your own swap, it all depends on context. In your example, it is most likely unnecessary, but your class' logic might be more complex.

Let's say you have a class that holds a pointer to some data structure, and a lot of metrics associated with that structure, each of them takes a long time to compute, and requires a lot of space to store, and these metrics are used as temporaries when doing some computations over the data (I know the following example is probably a crappy designed class, but it should illustrate the idea):

class MyClass
{
public:
   void doSomeWork()
   {
       //uses _metricsOneValue and _metricsTwoValue as temporaries,
       //calls other functions that use them as temporaries too, etc.
   }

private:
   //used only in doSomeWork and functions called by it.
   //Value overwritten each call.
   SomeBigClass _metricsOneValue; 
   SomeBigClass _metricsTwoValue;
   <...>
   SomeOtherClass * _data;
}

Now imagine you need to swap() two instances of this class. The most straightforward implementation will copy everything including the old metrics, which, in reality, are not needed at the moment, since they will be overwritten the next time you call doSomeWork() anyway. So in this case you can optimize the swap by just swapping the pointer to the data structure.

like image 149
SingerOfTheFall Avatar answered Nov 15 '22 12:11

SingerOfTheFall