Does return statement copy values

前端 未结 11 1807
夕颜
夕颜 2020-12-23 08:56

I am wondering about this because of scope issues. For example, consider the code

typedef struct {
    int x1;/*top*/
    int x2;/*bottom*/
    int id;
} sub         


        
11条回答
  •  一整个雨季
    2020-12-23 09:31

    I do not agree and NOT RECOMMEND to return vector to change values: Is much faster pass as reference:

    void vectorial(vector  a, vector  b, vector  &c)
    {
        c[0] = a[1] * b[2] - b[1] * a[2]; c[1] = -a[0] * b[2] + b[0] * a[2]; c[2] = a[0] * b[1] - b[0] * a[1];
    }
    //This is slow!!!:
    vector  vectorial(vector  a, vector  b)
    {
        vector  c{ a[1] * b[2] - b[1] * a[2], -a[0] * b[2] + b[0] * a[2], a[0] * b[1] - b[0] * a[1] };
        return c;
    }
    

    I tested on VS2015 with following results in release mode:

    By reference:8.01 MOPs and returning vector: 5.09 MOPs 60% worse!

    In debug mode things are much worse:

    By reference:0.053 MOPS and return vector: 0.034 MOPs

提交回复
热议问题