通过值或通过引用返回类对象,这在这里会更快

Returning class object by value or pass by reference, which will be faster here

本文关键字:在这里 引用 返回 对象      更新时间:2023-10-16

假设我有一个class对象matrix。对于add和两个大元素的matrix,我可以定义一个重载+的运算符,或者定义一个像这些一样的函数Add

matrix operator + (const matrix &A, const matrix &B)
matrix C;
/* all required things */
for(int i .........){
C(i)=A(i)+B(i);
}
return C;
}

我打电话给

matrix D = A+B; 

现在,如果我定义Add函数,

void Add(const matrix &A, const matrix &B, matrix &C)
C.resize(); // according to dimensions of A, B
// for C.resize , copy constructor will be called.
/* all required things */
for(int i .........){
C(i)=A(i)+B(i);
}
}

我必须调用这个函数,比如

matrix D;
Add(A,B,D); //D=A+B

上述方法中的哪一种更快、更有效。我们应该使用哪一个?

  1. 在不使用任何工具的情况下,
    1. 类似于探查器(例如gprof),以查看在哪里花费了多少时间
    2. 也没有任何其他工具,如">valgrind+cachegrind",以查看在这两个功能中执行了多少操作
  2. 并且还忽略所有编译器优化,即使用CCD_
  3. 假设这两个函数中还有其他任何东西(表示为/* all required things */)都是微不足道的

那么,只要看看你的两个函数,就可以说,你的两种函数的复杂性都是O(n),因为你的两款函数大部分时间都在两个for循环中。根据矩阵的大小,特别是如果它们真的很大,代码中的其他一切在降低速度时都是微不足道的。


所以,在我看来,你的问题归结为

  1. 需要多少时间,
    1. 调用C的构造函数
    2. 加上返回该CCD_ 13,与之相比
  2. 需要多少时间,
    1. 为CCD_ 15调用CCD_
    2. 再加上调用CCD_ 16的复制构造函数

这可以使用std::clock()chrono"粗略但相对快速"地进行测量,如多个答案所示。

#include <chrono>
auto t_start = std::chrono::high_resolution_clock::now();
matrix D = A+B; // To compare replace on 2nd run with this --->   matrix D; Add(A,B,D);
auto t_end = std::chrono::high_resolution_clock::now();
double elaspedTimeMs = std::chrono::duration<double, std::milli>(t_end-t_start).count();

不过,在我看来,如果你的矩阵很大,大部分时间都会进入for循环。

p.s.过早的优化是万恶之源