List memory usage

前端 未结 2 947
[愿得一人]
[愿得一人] 2021-01-11 14:03

I am trying to improve the memory usage of my script in python, therefore I need to know what\'s RAM usage of my list. I measure the memory usage with

print          


        
2条回答
  •  没有蜡笔的小新
    2021-01-11 14:32

    Here is a code snippet that shows two suggested answers. However, the use of pympler gives what I believe to be the correct and more succinct answer to my question. Thank you falsetru :-)

    import sys
    from pympler.asizeof import asizeof
    
    tuple1 = ('1234','2019-04-27','23.4658')
    tuple2 = ('1563','2019-04-27','19.2468')
    klist1 = [tuple1]
    klist2 = [tuple1,tuple2]
    # The results for the following did not answer my question 
    print ("sys.getsizeof(klist1): ",sys.getsizeof(klist1))
    print ("sys.getsizeof(klist2): ",sys.getsizeof(klist2))    
    # The results for the following give a quite reasonable answer
    print ("asizeof(klist1): ",asizeof(klist1))
    print ("asizeof(klist2): ",asizeof(klist2))
    

提交回复
热议问题