Primeira vez aqui? Seja bem vindo e cheque o FAQ!
x

Como o python aloca dinamicamente memória para uma lista?

0 votos
1,372 visitas

1 Resposta

0 votos
respondida Abr 4, 2016 por danielcajueiro (5,661 pontos)  

Ele tenta explorar a idéia do custo amortizado (médio) de aumento do tamanho. O programa abaixo dá uma idéia do que ocorre:

import sys        

if __name__ == '__main__':

    myList=[]
    n=64
    for i in range(64):
        sizeList=len(myList)
        sizeMemory=sys.getsizeof(myList)
        print("Length: ", sizeList, "Size Memory", sizeMemory)
        myList.append(None)

A output desse programa é:

Length:  0 Size Memory 64
Length:  1 Size Memory 96
Length:  2 Size Memory 96
Length:  3 Size Memory 96
Length:  4 Size Memory 96
Length:  5 Size Memory 128
Length:  6 Size Memory 128
Length:  7 Size Memory 128
Length:  8 Size Memory 128
Length:  9 Size Memory 192
Length:  10 Size Memory 192
Length:  11 Size Memory 192
Length:  12 Size Memory 192
Length:  13 Size Memory 192
Length:  14 Size Memory 192
Length:  15 Size Memory 192
Length:  16 Size Memory 192
Length:  17 Size Memory 264
Length:  18 Size Memory 264
Length:  19 Size Memory 264
Length:  20 Size Memory 264
Length:  21 Size Memory 264
Length:  22 Size Memory 264
Length:  23 Size Memory 264
Length:  24 Size Memory 264
Length:  25 Size Memory 264
Length:  26 Size Memory 344
Length:  27 Size Memory 344
Length:  28 Size Memory 344
Length:  29 Size Memory 344
Length:  30 Size Memory 344
Length:  31 Size Memory 344
Length:  32 Size Memory 344
Length:  33 Size Memory 344
Length:  34 Size Memory 344
Length:  35 Size Memory 344
Length:  36 Size Memory 432
Length:  37 Size Memory 432
Length:  38 Size Memory 432
Length:  39 Size Memory 432
Length:  40 Size Memory 432
Length:  41 Size Memory 432
Length:  42 Size Memory 432
Length:  43 Size Memory 432
Length:  44 Size Memory 432
Length:  45 Size Memory 432
Length:  46 Size Memory 432
Length:  47 Size Memory 528
Length:  48 Size Memory 528
Length:  49 Size Memory 528
Length:  50 Size Memory 528
Length:  51 Size Memory 528
Length:  52 Size Memory 528
Length:  53 Size Memory 528
Length:  54 Size Memory 528
Length:  55 Size Memory 528
Length:  56 Size Memory 528
Length:  57 Size Memory 528
Length:  58 Size Memory 528
Length:  59 Size Memory 640
Length:  60 Size Memory 640
Length:  61 Size Memory 640
Length:  62 Size Memory 640
Length:  63 Size Memory 640
...