Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of python when passing big data as function parameter

Tags:

Suppose I have two Python functions, Function 1 and Function 2.

Function 1 will call Function 2 and the parameter is a big data (e.g., a dictionary with 100 thousand elements).

I am wondering if there is any performance differences between calling Function 2 in Function 1, which means I need to pass the big data parameter, and implementing Function 2 in Function 1 directly, which means I don't need to pass the big data parameter.

Thanks.

PS: I think the key question is how Python pass the parameter, by value, or by reference (pointer)?

Edit: It seems that this is a confused problem. How do I pass a variable by reference? is a good answer.

like image 710
mitchelllc Avatar asked May 09 '13 22:05

mitchelllc


People also ask

Are functions slower in Python?

A function never makes those statements themselves slower. A Python function is just an object stored in a variable; you can assign functions to a different variable, replace them with something completely different, or delete them at any time.

Can you pass function as parameter in Python?

Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables. In fact, you can think about a method (or function) as a variable whose value is the actual callable code object.

Does Python pass parameters by value?

Python function arguments pass by reference or valueThe parameters in the python language are passed by reference. Which mean if we change what parameter refers to within the function, the change also reflect black in the calling function.


2 Answers

Python passes references-to-objects by value. The terminology is controversial and ugly, but there should be no real performance difference.

Check out these answers for all the details you could ever want (hopefully).

like image 139
Henry Keiter Avatar answered Oct 11 '22 10:10

Henry Keiter


The terminology of how python "passes" is a vicious debate I don't want to go into. But what is actually passed on the stack is a reference. So there is not a large memory cost with either of your options.

like image 32
cmd Avatar answered Oct 11 '22 10:10

cmd