Performance when passing huge list as argument in recursive function?

后端 未结 2 700
轮回少年
轮回少年 2021-01-14 15:41

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         


        
2条回答
  •  天命终不由人
    2021-01-14 16:19

    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.

提交回复
热议问题