将多个参数传递给函数的最有效方法?

时间:2021-04-08 11:00:30

What is the most efficient (in terms of processing speed and memory utilisation) method for passing a large number of user-input variables as arguments to a function, and for returning multiple results?

什么是最有效的(在处理速度和内存利用率方面)方法,用于将大量用户输入变量作为参数传递给函数,以及返回多个结果?

A long string of arguments and return values each time I call the function - e.g. (a,b,c,d,e,f,g) = MyFunction(a,b,c,d,e,f,g) - seems inelegant, and I'm guessing is also inefficient; especially if I have to call the function repeatedly or recursively.

每次调用函数时都有一长串参数和返回值 - 例如(a,b,c,d,e,f,g)= MyFunction(a,b,c,d,e,f,g) - 似乎不优雅,我猜也是低效的;特别是如果我必须重复或递归地调用该函数。

However defining the whole list of variables as Global outside of the function also is ugly, and carries the danger of variable names being inadvertently assigned to several different variables as my program grows.

然而,将整个变量列表定义为函数外的全局变量也很难看,并且随着程序的增长,变量名称会无意中被分配给几个不同的变量。

I've tried putting all the variables into a single array or list and passed that to the function as a single argument, as this seems neater. Am I correct in thinking that this is also more efficient, even for huge arrays, since it is only the pointer to the start of the array that is passed to the function each time, not the whole array itself? If arrays are the best method for passing a large number of variables to/from a function, at what point does this efficiency saving kick in - e.g. is it better to pass a string of arguments if the number of arguments is less than 5, but use an array or list if 5 or more arguments are required?

我已经尝试将所有变量放入单个数组或列表中,并将其作为单个参数传递给函数,因为这似乎更整洁。我是否认为这也更有效,即使对于大型数组也是如此,因为它只是指向每次传递给函数的数组开头的指针,而不是整个数组本身?如果数组是向函数传递大量变量或从函数传递大量变量的最佳方法,那么这种效率节省在什么时候开始 - 例如如果参数的数量小于5,最好传递一串参数,但如果需要5个或更多参数,则使用数组或列表?

A previous discussion on StackExchange: Elegant way to pass multiple arguments to a function has recommended using struct rather than vectors/arrays for passing multiple arguments. Why is this method preferred to using arrays, and at what point do efficiency savings justify the added complexity of using struct?

之前关于StackExchange的讨论:将多个参数传递给函数的优雅方法建议使用struct而不是vectors / arrays来传递多个参数。为什么这种方法更倾向于使用数组,并且在什么时候效率节省证明了使用struct的额外复杂性?

Are there any other methods that I should consider which will work in Python or C/C++? (e.g. I'm new to object orientated programming, but wonder if this might offer a solution which is specific to Python?)

我还应该考虑哪些方法可以在Python或C / C ++中使用? (例如,我是面向对象编程的新手,但想知道这是否可以提供特定于Python的解决方案?)

Many thanks

2 个解决方案

#1


2  

All of this depends on the target system and its calling convention for functions. This answer applies to C and C++ only.

所有这些都取决于目标系统及其函数的调用约定。这个答案仅适用于C和C ++。

Generally, the use of file scope variables will usually be the fastest possible. In such cases, the variable should never be declared as global (accessible throughout the whole project), but as static (accessible by the local file only).

通常,文件范围变量的使用通常是最快的。在这种情况下,变量不应该被声明为全局(在整个项目中可访问),而应该是静态的(仅由本地文件访问)。

Still, such static file scope variables should be avoided for several reasons: they can make the code harder to read and maintain, indisciplined use may lead to "spaghetti code", they will create re-entrancy issues and they add some extra identifiers to the file scope namespace.

仍然应该避免使用这样的静态文件范围变量,原因如下:它们会使代码更难以阅读和维护,无纪律的使用可能导致“意大利面条代码”,它们会产生重新入侵问题,并且它们会增加一些额外的标识符。文件范围名称空间

It should be noted, that in case the number of parameters are limited, that keeping them as separate parameters might increase performance, as the compiler may then store some of them in CPU registers instead of storing them on the stack. CPU registers are the fastest way of passing parameters to a function. How this works is very system-specific. However, writing your program in such a manner that you hope to get the parameters passed through CPU registers, is pre-mature optimization in most cases.

应该注意的是,如果参数的数量有限,将它们保持为单独的参数可能会提高性能,因为编译器可能会将其中一些存储在CPU寄存器中而不是将它们存储在堆栈中。 CPU寄存器是将参数传递给函数的最快方法。这是如何工作的是特定于系统的。但是,在大多数情况下,以希望通过CPU寄存器传递参数的方式编写程序是预先成熟的优化。

The best, de facto way of passing multiple arguments is indeed to create a custom struct (or C++ class) containing all of the arguments. This structure is then passed by reference to the function. Try to make it so that the struct contains only variables related to each other. Consider putting variables that are not related to each other, or special just for one given function, in a separate parameter. Good program design supersedes efficiency in most cases.

传递多个参数的最佳,事实上的方法确实是创建一个包含所有参数的自定义结构(或C ++类)。然后通过引用函数传递该结构。尝试使结构只包含彼此相关的变量。考虑在一个单独的参数中放置彼此不相关的变量,或者仅为一个给定函数设置特殊变量。在大多数情况下,良好的程序设计会取代效率。

The reason why a struct/class is preferable instead of an array, is simply because the variables together form a unique type, but also since they will likely have different types compared to each other. Making an array of variables that all have different types doesn't make any sense.

结构/类是优选而不是数组的原因仅仅是因为变量一起形成唯一类型,但也因为它们可能具有彼此不同的类型。制作一个所有具有不同类型的变量数组没有任何意义。

And in C++, a class offers other advantages over an array, such as constructors and destructors, custom assignment operators etc.

在C ++中,类提供了与数组相比的其他优点,例如构造函数和析构函数,自定义赋值运算符等。

#2


0  

It will obviously depend on what you want to do, because each of the containers has a different purpose.

它显然取决于你想做什么,因为每个容器都有不同的用途。

For sure, in term of processing speed and memory, you should use a pointer or a reference to a container (Structure, class, array, tuple...), in order to not copy all the data but just the address of the container.

当然,在处理速度和内存方面,你应该使用指针或对容器的引用(结构,类,数组,元组...),以便不复制所有数据,只复制容器的地址。

However, you must not create a structure, or put all your variables in the same container just in order to give them as a parameter of a function. All the variables that you will put on the data structure should be related.

但是,您不能创建结构,也不能将所有变量放在同一个容器中,以便将它们作为函数的参数。您将在数据结构上放置的所有变量都应该是相关的。

In the example that you gave, there are multiple variable of different types. That is why a structure is preferred, because an array requires that all parameters have the same type. In python you could use named tuple in order to store different variable.

在您给出的示例中,有多个不同类型的变量。这就是首选结构的原因,因为数组要求所有参数都具有相同的类型。在python中,您可以使用命名元组来存储不同的变量。

#1


2  

All of this depends on the target system and its calling convention for functions. This answer applies to C and C++ only.

所有这些都取决于目标系统及其函数的调用约定。这个答案仅适用于C和C ++。

Generally, the use of file scope variables will usually be the fastest possible. In such cases, the variable should never be declared as global (accessible throughout the whole project), but as static (accessible by the local file only).

通常,文件范围变量的使用通常是最快的。在这种情况下,变量不应该被声明为全局(在整个项目中可访问),而应该是静态的(仅由本地文件访问)。

Still, such static file scope variables should be avoided for several reasons: they can make the code harder to read and maintain, indisciplined use may lead to "spaghetti code", they will create re-entrancy issues and they add some extra identifiers to the file scope namespace.

仍然应该避免使用这样的静态文件范围变量,原因如下:它们会使代码更难以阅读和维护,无纪律的使用可能导致“意大利面条代码”,它们会产生重新入侵问题,并且它们会增加一些额外的标识符。文件范围名称空间

It should be noted, that in case the number of parameters are limited, that keeping them as separate parameters might increase performance, as the compiler may then store some of them in CPU registers instead of storing them on the stack. CPU registers are the fastest way of passing parameters to a function. How this works is very system-specific. However, writing your program in such a manner that you hope to get the parameters passed through CPU registers, is pre-mature optimization in most cases.

应该注意的是,如果参数的数量有限,将它们保持为单独的参数可能会提高性能,因为编译器可能会将其中一些存储在CPU寄存器中而不是将它们存储在堆栈中。 CPU寄存器是将参数传递给函数的最快方法。这是如何工作的是特定于系统的。但是,在大多数情况下,以希望通过CPU寄存器传递参数的方式编写程序是预先成熟的优化。

The best, de facto way of passing multiple arguments is indeed to create a custom struct (or C++ class) containing all of the arguments. This structure is then passed by reference to the function. Try to make it so that the struct contains only variables related to each other. Consider putting variables that are not related to each other, or special just for one given function, in a separate parameter. Good program design supersedes efficiency in most cases.

传递多个参数的最佳,事实上的方法确实是创建一个包含所有参数的自定义结构(或C ++类)。然后通过引用函数传递该结构。尝试使结构只包含彼此相关的变量。考虑在一个单独的参数中放置彼此不相关的变量,或者仅为一个给定函数设置特殊变量。在大多数情况下,良好的程序设计会取代效率。

The reason why a struct/class is preferable instead of an array, is simply because the variables together form a unique type, but also since they will likely have different types compared to each other. Making an array of variables that all have different types doesn't make any sense.

结构/类是优选而不是数组的原因仅仅是因为变量一起形成唯一类型,但也因为它们可能具有彼此不同的类型。制作一个所有具有不同类型的变量数组没有任何意义。

And in C++, a class offers other advantages over an array, such as constructors and destructors, custom assignment operators etc.

在C ++中,类提供了与数组相比的其他优点,例如构造函数和析构函数,自定义赋值运算符等。

#2


0  

It will obviously depend on what you want to do, because each of the containers has a different purpose.

它显然取决于你想做什么,因为每个容器都有不同的用途。

For sure, in term of processing speed and memory, you should use a pointer or a reference to a container (Structure, class, array, tuple...), in order to not copy all the data but just the address of the container.

当然,在处理速度和内存方面,你应该使用指针或对容器的引用(结构,类,数组,元组...),以便不复制所有数据,只复制容器的地址。

However, you must not create a structure, or put all your variables in the same container just in order to give them as a parameter of a function. All the variables that you will put on the data structure should be related.

但是,您不能创建结构,也不能将所有变量放在同一个容器中,以便将它们作为函数的参数。您将在数据结构上放置的所有变量都应该是相关的。

In the example that you gave, there are multiple variable of different types. That is why a structure is preferred, because an array requires that all parameters have the same type. In python you could use named tuple in order to store different variable.

在您给出的示例中,有多个不同类型的变量。这就是首选结构的原因,因为数组要求所有参数都具有相同的类型。在python中,您可以使用命名元组来存储不同的变量。