Home > Article > Backend Development > The impact of C++ function parameter passing methods on program performance
In C, function parameters can be passed by value (creating a copy to pass to the function) or by reference (passing the address of the original variable). Passing by value is safe but expensive and is suitable for small parameters or when the original variable needs to be protected. Reference passing is fast but has low flexibility and is suitable for large objects or situations where the original variable needs to be modified. Experiments show that when dealing with large arrays, passing by reference significantly improves performance than passing by value.
C The impact of function parameter passing method on program performance
In C, function parameters can be passed by value or by reference Delivered in two ways. For different parameter types, it is crucial to choose the appropriate delivery method because it will significantly affect the performance of the program.
Pass by value
In pass by value, a copy of the function parameters is passed to the function. Changing the copy does not affect the value of the original variable. Although passing by value provides greater safety, there is an overhead when passing large objects because parameters need to be created and copied. The following code example demonstrates pass-by-value:
void foo(int num) { num++; // 值传递副本,不会影响原始变量 } int main() { int x = 5; foo(x); cout << x << endl; // 输出5,没有改变 }
Pass-by-reference
In pass-by-reference, a reference to a function parameter is passed to the function. This means that the function receives the address of the original variable. Changes made to the reference will directly affect the value of the original variable. Pass-by-reference eliminates the overhead of passing by value because it does not require copying parameters, but it sacrifices flexibility because modification of the variable pointed to by the reference is not allowed. The following code example demonstrates passing by reference:
void foo(int& num) { num++; // 引用传递引用,更改原始变量 } int main() { int x = 5; foo(x); cout << x << endl; // 输出6,已改变 }
Selective passing method
Choosing the best passing method depends on the situation:
Practical case
In the following example, we compare program performance when passing an array as a value and when passing a reference to a function:
#include <chrono> #include <vector> int sum_array_by_value(const std::vector<int>& arr) { int sum = 0; for (int i : arr) { sum += i; } return sum; } int sum_array_by_reference(std::vector<int>& arr) { int sum = 0; for (int& i : arr) { sum += i; } return sum; } int main() { std::vector<int> arr(1000000); for (int i = 0; i < arr.size(); i++) arr[i] = i; auto start = std::chrono::high_resolution_clock::now(); int sum1 = sum_array_by_value(arr); auto end = std::chrono::high_resolution_clock::now(); int elapsed1 = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); start = std::chrono::high_resolution_clock::now(); int sum2 = sum_array_by_reference(arr); end = std::chrono::high_resolution_clock::now(); int elapsed2 = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); std::cout << "Sum by value: " << elapsed1 << " ms" << std::endl; std::cout << "Sum by reference: " << elapsed2 << " ms" << std::endl; return 0; }
While executing this program, we will observe that using pass by reference significantly improves performance since large arrays do not need to be copied.
The above is the detailed content of The impact of C++ function parameter passing methods on program performance. For more information, please follow other related articles on the PHP Chinese website!