3

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.

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?

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?

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?)

Many thanks

Community
  • 1
  • 1
Dave
  • 515
  • 1
  • 8
  • 17
  • 2
    Usually you'd pass a struct containing the arguments. You could pass a tuple I suppose (more idiomatic for C++ but still not quite there yet in my opinion). – Robinson Aug 19 '15 at 08:21
  • 4
    Specify the language, the seemingly same question are actually quite different for different languages. – Yu Hao Aug 19 '15 at 08:24
  • Pass in a tuple or a dict or your own custom object – muddyfish Aug 19 '15 at 08:24
  • Passing a reference to a struct or an array would be the most efficient way of passing multiple arguments, as well as changing them in place. – bgeschka Aug 19 '15 at 08:25
  • @Dave: array requires that all parameters have same type. `struct` doesn't have this restriction. – Jarod42 Aug 19 '15 at 08:34
  • Thanks all for your rapid responses & clear explanations. All upvoted & marked "Answered". BW. Dave – Dave Aug 19 '15 at 13:43

2 Answers2

2

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

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.

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.

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
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.

Joyas
  • 433
  • 3
  • 6