如何在C ++ 11中高效地返回大数据 [英] How to return large data efficiently in C++11

查看:149
本文介绍了如何在C ++ 11中高效地返回大数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑,在C ++ 11中返回大数据。什么是最有效的方法?
这是我的相关函数:

  void numericMethod1(vector< double>& solution,
const双输入);

void numericMethod2(pair< vector< double>,vector< double>& solution1,
vector< double>& solution2,
const double input1,
const double input2);

这里是我使用它们的方式:

  int main()
{
// apply numericMethod1
double input = 0;
矢量< double>解;
numericMethod1(solution,input);

// apply numericMethod2
double input1 = 1;
double input2 = 2;
pair< vector< double>,vector< double>> solution1;
矢量< double> solution2;
numericMethod2(solution1,solution2,input1,input2);

return 0;问题是,std :: move()没有用处,因为它是一个非常简单的方法。



  void numericMethod1(vector< double>& solution,
const double input)
{
vector< double> tmp_solution;

for(...)
{
//关于tmp_solution的一些操作
//之后这个向量变得非常大
}

solution = std :: move(tmp_solution);
}

void numericMethod2(pair< vector< double>,vector< double>& solution1,
vector< double> solution2,
const double input1,
const double input2)
{
vector< double> tmp_solution1_1;
矢量< double> tmp_solution1_2;
矢量< double> tmp_solution2;

for(...)
{
//关于tmp_solution1_1,tmp_solution1_2和tmp_solution2
的一些操作//之后,三个向量变得非常大
}

solution1.first = std :: move(tmp_solution1_1);
solution1.second = std :: move(tmp_solution1_2);
solution2 = std :: move(tmp_solution2);
}

如果它们没用,我如何处理这些大的返回值没有副本多次?
可自由更改API!



UPDATE



感谢StackOverFlow和这些答案,在潜入相关问题后,我更好地了解这个问题。由于RVO,我更改了API,为了更清楚,我不再使用std :: pair了。这里是我的新代码:

  struct SolutionType 
{
vector< double& X;
矢量< double> Y;
};

SolutionType newNumericMethod(const double input1,
const double input2);

int main()
{
// apply newNumericMethod
double input1 = 1;
double input2 = 2;
SolutionType solution = newNumericMethod(input1,input2);

return 0;
}

SolutionType newNumericMethod(const double input1,
const double input2);
{
SolutionType tmp_solution; //这将调用默认构造函数,对不对?
//因为名字太长,我做别名。
矢量< double> & x = tmp_solution.X;
矢量< double> & y = tmp_solution.Y;

for(...)
{
//关于x和y的一些操作
//之后这两个向量变得非常大
}

return tmp_solution;
}

我如何知道RVO发生了?

解决方案

按值返回,请依赖 RVO(返回值优化)

  auto make_big_vector()
{
vector< huge_thing> v1;
//填充v1

//这里不需要显式移动
return v1;
}

auto make_big_stuff_tuple()
{
vector< double> v0;
// fill v0

vector< huge_thing> v1;
// fill v1

// make_tuple的参数需要显式移动,
//因为make_tuple使用了完美转发:
// http:// en .cppreference.com / w / cpp / utility / tuple / make_tuple

return std :: make_tuple(std :: move(v0),std :: move(v1));
}

auto r0 = make_big_vector();
auto r1 = make_big_stuff_tuple();

我会更改你的函数的API以简单地返回值。


I'm realy confused about returning large data in C++11. What is the most efficient way? Here is my related function:

void numericMethod1(vector<double>& solution,
                    const double input);

void numericMethod2(pair<vector<double>,vector<double>>& solution1,
                    vector<double>& solution2,
                    const double input1,
                    const double input2);

and here is the way i use them:

int main()
{
    // apply numericMethod1
    double input = 0;
    vector<double> solution;
    numericMethod1(solution, input);

    // apply numericMethod2
    double input1 = 1;
    double input2 = 2;
    pair<vector<double>,vector<double>> solution1;
    vector<double> solution2;
    numericMethod2(solution1, solution2, input1, input2);

    return 0;
}

The question is, is the std::move() useless in following implemtation?

Implementation:

void numericMethod1(vector<double>& solution,
                    const double input)
{
    vector<double> tmp_solution;

    for (...)
    {
    // some operation about tmp_solution
    // after that this vector become very large
    }

    solution = std::move(tmp_solution);
}

void numericMethod2(pair<vector<double>,vector<double>>& solution1,
                    vector<double>& solution2,
                    const double input1,
                    const double input2)
{
    vector<double> tmp_solution1_1;
    vector<double> tmp_solution1_2;
    vector<double> tmp_solution2;

    for (...)
    {
    // some operation about tmp_solution1_1, tmp_solution1_2 and tmp_solution2
    // after that the three vector become very large
    }

    solution1.first = std::move(tmp_solution1_1);
    solution1.second = std::move(tmp_solution1_2);
    solution2 = std::move(tmp_solution2);
}

If they are useless, how can i deal with these large return value without copy many times? Free to change the API!

UPDATE

Thanks to StackOverFlow and these answers, after diving into related questions, I know this problem better. Due to RVO, I change the API, and for more clear, I don't use std::pair anymore. Here, is my new code:

struct SolutionType
{
    vector<double> X;
    vector<double> Y;
};

SolutionType newNumericMethod(const double input1,
                              const double input2);

int main()
{
    // apply newNumericMethod
    double input1 = 1;
    double input2 = 2;
    SolutionType solution = newNumericMethod(input1, input2);

    return 0;
}

SolutionType newNumericMethod(const double input1,
                              const double input2);
{
    SolutionType tmp_solution; // this will call the default constructor, right?
    // since the name is too long, i make alias.
    vector<double> &x = tmp_solution.X;
    vector<double> &y = tmp_solution.Y;

    for (...)
    {
    // some operation about x and y
    // after that these two vectors become very large
    }

    return tmp_solution;
}

How can I know RVO is happened? or How can I ensure RVO happened?

解决方案

Return by value, rely on RVO (return value optimization).

auto make_big_vector()
{
    vector<huge_thing> v1;
    // fill v1

    // explicit move is not necessary here        
    return v1;
} 

auto make_big_stuff_tuple()
{
    vector<double> v0;
    // fill v0

    vector<huge_thing> v1;
    // fill v1

    // explicit move is necessary for make_tuple's arguments,
    // as make_tuple uses perfect-forwarding:
    // http://en.cppreference.com/w/cpp/utility/tuple/make_tuple

    return std::make_tuple(std::move(v0), std::move(v1));
}

auto r0 = make_big_vector();
auto r1 = make_big_stuff_tuple();

I would change the API of your functions to simply return by value.

这篇关于如何在C ++ 11中高效地返回大数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆