Python Memory Model

前端 未结 4 1734
半阙折子戏
半阙折子戏 2020-12-24 09:00

I have a very large list Suppose I do that (yeah, I know the code is very unpythonic, but for the example\'s sake..):

n = (2**32)**2
for i in xrange(10**7)
          


        
4条回答
  •  生来不讨喜
    2020-12-24 09:50

    In your first example you are storing the same integer len(arr) times. So python need just store the integer once in memory and refers to it len(arr) times.

    In your second example, you are storing len(arr) different integers. Now python must allocate storage for len(arr) integers and refer to to them in each of the len(arr) slots.

提交回复
热议问题