在c語言中提供了sizeof可以用來查看一個變量占用的空間大小,在python中可以使用sys模塊下的getsizeof方法來判斷變量占用的空間大小。其文檔解釋如下:[mw_shl_code=python,true]sys.getsizeof(object[, default]):[/mw_shl_code]Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.The default argument allows to define a value which will be returned if the object type does not provide means to retrieve the size and would cause a TypeError.getsizeof calls the object’s sizeof method and adds an additional garbage collector overhead if the object is managed by the garbage collector.使用示例:[mw_shl_code=python,true]import sysv = 1print sys.getsizeof(v)s = 'abc'print sys.getsizeof(s)[/mw_shl_code]
1 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
sys.getsizeof(object[, default])
下面是我摘錄的,希望對你有用。
以字節(byte)為單位返回對象大小。 這個對象可以是任何類型的對象。 所以內置對象都能返回正確的結果 但不保證對第三方擴展有效,因為和具體實現相關。
getsizeof() 調用對象的 __sizeof__ 方法, 如果對象由垃圾收集器管理, 則會加上額外的垃圾收集器開銷。
添加回答
舉報
0/150
提交
取消