2018年1月9日 星期二

Python: Get the size of an object in bytes

Python: Get the size of an object in bytes

Python Basic: Exercise-79 with Solution

Write a Python program to get the size of an object in bytes.
Sample Solution:-
import sys
str1 = "one"
str2 = "four"
str3 = "three"
str4 = 124
str5 = 123.4
print()
print("Memory size of '"+str1+"' = "+str(sys.getsizeof(str1))+ " bytes")
print("Memory size of '"+str2+"' = "+str(sys.getsizeof(str2))+ " bytes")
print("Memory size of '"+str3+"' = "+str(sys.getsizeof(str3))+ " bytes")
print()
print("Memory size of '"+str(str4)+"' = "+str(sys.getsizeof(str4))+ " bytes")
print("Memory size of '"+str(str5)+"' = "+str(sys.getsizeof(str5))+ " bytes")

============ RESTART: F:/Python_APSC/py-ex-basic-79.py ===========

Memory size of 'one' = 28 bytes
Memory size of 'four' = 29 bytes
Memory size of 'three' = 30 bytes

Memory size of '124' = 14 bytes
Memory size of '123.4' = 16 bytes
>>>

沒有留言:

張貼留言

Node-Red --> MQTT --> Fuxa 開源碼網頁式圖控平台

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...