使用std::交换向量或向量::交换?

时间:2022-09-06 12:35:02

Given two std::vector v1, v2.
I was wondering what are the benefits to use std::swap(v1, v2) over v1.swap(v2).

给定两个std::向量v1 v2。我想知道使用std的好处是什么:交换(v1, v2)除以v1.swap(v2)。

I have implemented a simple test code (I am not sure it is pertinent) regarding performance point of view :

关于性能方面,我实现了一个简单的测试代码(我不确定它是否相关):

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>

#define N 100000

template<typename TimeT = std::chrono::microseconds>
struct Timer
{
    template<typename F, typename ...Args>
    static typename TimeT::rep exec(F func, Args&&... args)
    {
        auto start = std::chrono::steady_clock::now();
        func(std::forward<Args>(args)...);
        auto duration = std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now() - start);
        return duration.count();
    }
};

void test_std_swap(std::vector<double>& v1, std::vector<double>& v2)
{
    for (int i = 0; i < N; i ++)
    {
        std::swap(v1,v2);
        std::swap(v2,v1);
    }
}

void test_swap_vector(std::vector<double>& v1, std::vector<double>& v2)
{
    for (int i = 0; i < N; i ++)
    {
        v1.swap(v2);
        v2.swap(v1);
    }
}

int main()
{
    std::vector<double> A(1000);
    std::generate( A.begin(), A.end(), [&]() { return std::rand(); } );
    std::vector<double> B(1000);
    std::generate( B.begin(), B.end(), [&]() { return std::rand(); } );
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_std_swap, A, B) << std::endl;
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_swap_vector, A, B)  << std::endl;
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_std_swap, A, B) << std::endl;
    std::cout << Timer<>::exec<void(std::vector<double>& v1, std::vector<double>& v2)>(test_swap_vector, A, B)  << std::endl;
}

According to outputs it seems that vector::swap seems faster without optimization -O0. Output is (in microseconds) :

根据输出,如果没有优化-O0,那么vector:::swap似乎更快。输出为(微秒):

20292
16246
16400
13898

And with -O3 there is no revelant difference.

与-O3没有明显区别。

752
752
752
760

2 个解决方案

#1


6  

You should not use std::swap() directly in any case! Instead, you should use something like this:

您不应该使用std::swap()直接在任何情况下!相反,你应该使用这样的东西:

using std::swap;
swap(x, y);

For std::vector<...> it probably doesn't make a difference as std::vector<...> obviously lives in namespace std. Otherwise the key difference is that with using std::swap() the default implementation is being used while with the approach outlined about ADL can find a better version.

std::向量<…它可能与std::vector<…>显然存在于命名空间std中,否则关键区别在于使用std::swap()时使用默认实现,而使用描述ADL的方法可以找到更好的版本。

Using swap(x, y) for std::vector<...>s x and y will just call x.swap(y). For consistency with other uses I would use the approach listed above.

用(x, y)交换std::vector<…>sx和y就叫x。swap(y)为了与其他用途保持一致,我将使用上面列出的方法。

#2


9  

Assuming a sane implementation, both of those functions should be implemented identically. So you should use whatever is most readable in your code.

假设实现是正常的,那么这两个功能都应该实现一致。因此,您应该使用代码中最易读的内容。

In particular, if we look at the description for std::swap(vector<T> & x, vector<T> & y), it's effect is x.swap(y).

特别地,如果我们看一下std::swap的描述(向量 & x,向量 & y),它的效果是x.swap(y)。

#1


6  

You should not use std::swap() directly in any case! Instead, you should use something like this:

您不应该使用std::swap()直接在任何情况下!相反,你应该使用这样的东西:

using std::swap;
swap(x, y);

For std::vector<...> it probably doesn't make a difference as std::vector<...> obviously lives in namespace std. Otherwise the key difference is that with using std::swap() the default implementation is being used while with the approach outlined about ADL can find a better version.

std::向量<…它可能与std::vector<…>显然存在于命名空间std中,否则关键区别在于使用std::swap()时使用默认实现,而使用描述ADL的方法可以找到更好的版本。

Using swap(x, y) for std::vector<...>s x and y will just call x.swap(y). For consistency with other uses I would use the approach listed above.

用(x, y)交换std::vector<…>sx和y就叫x。swap(y)为了与其他用途保持一致,我将使用上面列出的方法。

#2


9  

Assuming a sane implementation, both of those functions should be implemented identically. So you should use whatever is most readable in your code.

假设实现是正常的,那么这两个功能都应该实现一致。因此,您应该使用代码中最易读的内容。

In particular, if we look at the description for std::swap(vector<T> & x, vector<T> & y), it's effect is x.swap(y).

特别地,如果我们看一下std::swap的描述(向量 & x,向量 & y),它的效果是x.swap(y)。