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
>>>

沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...