Performance when passing huge list as argument in recursive function?

浪子不回头ぞ 提交于 2019-12-01 06:19:06

问题


I am using Python and I have a recursive function that takes a huge list as one of the arguments:

# Current implementation
def MyFunction(arg1, arg2, my_huge_list)
    ...
    ...
    MyFunction(new_arg1, new_arg2, my_huge_list)

As you can see above, MyFunction is called recursively using the same list my_huge_list; this doesn't change, unlike the other arguments. And, again, this list is huge. A friend of mine suggested that I could treat my_huge_list as a global variable to improve the performance, as otherwise this huge list may be copied over and over in every iteration.

# Friend's suggestion

MyHugeList=[a,b,c, ...and many many other elements... ]

def MyFunction(arg1, arg2)
    global MyHugeList
    ...
    ...
    MyFunction(new_arg1, new_arg2)

Does using a global variable as shown above improve the performance of the algorithm over the original version? My program runs for weeks, so even a slight improvement may be valuable in the long run.


回答1:


The list will be passed by reference, so it doesn't take any longer to transfer a 1-item list vs. a 100000 item list:

def null(x): return x
longlist = range(100000)
shortlist = range(1)
longerlist = range(1000000)

%timeit null(shortlist)
10000000 loops, best of 3: 124 ns per loop

%timeit null(longlist)
10000000 loops, best of 3: 137 ns per loop

%timeit null(longerlist)
10000000 loops, best of 3: 125 ns per loop

The longer lists have 100k and 1M entries in them, and yet don't take significantly long to pass as arguments than shorter lists.

there may be other ways to improve performance; this probably isn't one of them.




回答2:


No, arguments in Python are passed by reference.
More precisely - variables in Python is just a pointers that stores memory-addresses of actual data. So when Pythons variable-pointer passed to a function - it passed by its value - address pointing to actual data, it means that, variables passed to functions by value and value of variables are references to objects.

More on that topic:

  • http://testingreflections.com/node/5126
  • http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables


来源:https://stackoverflow.com/questions/19187785/performance-when-passing-huge-list-as-argument-in-recursive-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!