问题
Is it not true that depending on the language that way the parameters/arguments are passed will have a different time complexity. Then why is this not factored in or considered in the algorithms or programs that books measure time complexity of? CLRS or Data Structures and Algorithm Analysis by Mark Allen Weiss never would add the time complexity of how the arguments are being passed for the total runtime of the program? Am I misunderstanding something? I know CLRS was pseudocode but the Algorithm Analysis by Mark Allen Weiss showed code specific to Java.
回答1:
If I understand your question correctly, you're asking the following:
Some programming languages (most notably, C and C++) make a distinction between passing by value (making a copy of the arguments) and passing by pointer or by reference (in which the arguments aren't copied). The choice of how you pass parameters into a function changes how much work is done by the function, since if you pass by value you need to copy the arguments. However, CLRS doesn't talk about this at all, and books in Java don't address this because they're written in Java. Is this important, and should we care about it?
You are correct that the mode of parameter passing absolutely makes a difference in how long the function takes to run. For example, here's a not great implementation of binary search written in C++:
/* Bad code, please don't use this. */
bool binarySearchFor(std::vector<int> values, int key) {
std::size_t lhs = 0, rhs = values.size();
while (lhs < rhs) {
std::size_t mid = lhs + (rhs - lhs) / 2;
if (values[mid] == key) return true;
else if (values[mid] > key) lhs = mid;
else rhs = mid + 1;
}
return false;
}
The logic inside this function will run in time O(log n), where n is the number of elements in the std::vector
. However, since the vector is passed by value, simply copying that vector will take time Θ(n), dwarfing the actual algorithm's runtime and making the whole thing run in time Θ(n). On the other hand, changing the function so that we accept values
by const
reference drops the runtime down to O(log n).
So, why doesn't CLRS - or most other algorithms books - talk about this? One of the reasons why is that pass-by-value isn't a feature of all programming languages (or at least, not in the same way that it is in C++). In Python, JavaScript, Java, LISP, etc., parameters are technically passed by value, but since those parameters are references, the cost of passing the parameter is always O(1). So one reason not to talk about this in an algorithms book is that those books talk about the high-level conceptual algorithms followed rather than the actual code to power them, and therefore don't account for all details found in all programming languages. It's expected that you, the Well-Dressed and Wise Reader, will translate their algorithms into your programming language of choice, making appropriate use of the available language features.
来源:https://stackoverflow.com/questions/65205931/why-are-not-the-way-parameters-arguments-passed-considered-for-the-time-complexi