在c語言中提供了sizeof可以用來查看一個(gè)變量占用的空間大小,在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 回答

翻翻過去那場(chǎng)雪
TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
sys.getsizeof(object[, default])
下面是我摘錄的,希望對(duì)你有用。
以字節(jié)(byte)為單位返回對(duì)象大小。 這個(gè)對(duì)象可以是任何類型的對(duì)象。 所以內(nèi)置對(duì)象都能返回正確的結(jié)果 但不保證對(duì)第三方擴(kuò)展有效,因?yàn)楹途唧w實(shí)現(xiàn)相關(guān)。
getsizeof() 調(diào)用對(duì)象的 __sizeof__ 方法, 如果對(duì)象由垃圾收集器管理, 則會(huì)加上額外的垃圾收集器開銷。
添加回答
舉報(bào)
0/150
提交
取消