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 sysstr1 = "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")
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
>>>
沒有留言:
張貼留言