import os
import sys
import psutil
class CLASS_WITH_SLOTS():
__slots__ = ['i', 'name']
def __init__(self, i, name):
self.i = i
self.name = name
class CLASS_WITHOUT_SLOTS():
def __init__(self, i, name):
self.i = i
self.name = name
####################################
N = 900000
GB = 2.**30
for _ in range(0, 3):
# X 변수에 SLOTS이 있는 클래스 인스턴스를 만든다.
## WITHOUT SLOTS
print("== WITHOUT SLOTS")
X = [CLASS_WITHOUT_SLOTS(f"id{i}", f"name{i}") for i in range(0, N)]
print(f"memory Use: {CurrentProcess.memory_info()[0] / GB:9.4f} GB")
print(f"RAM usage Percent: {psutil.virtual_memory()[2]} %")
print("==" * 30)
####################################
# X 변수에 SLOTS이 **없는** 클래스 인스턴스를 만든다.
## WITH SLOTS
print("== WITH SLOTS")
X = [CLASS_WITH_SLOTS(f"id{i}", f"name{i}") for i in range(0, N)]
print(f"memory Use: {CurrentProcess.memory_info()[0] / GB :9.4f} GB")
print(f"RAM usage Percent: {psutil.virtual_memory()[2]} %")
print("==" * 30)
== WITHOUT SLOTS
memory Use: 0.2758 GB
RAM usage Percent: 79.1 %
============================================================
== WITH SLOTS
memory Use: 0.1696 GB
RAM usage Percent: 75.5 %
============================================================
== WITHOUT SLOTS
memory Use: 0.2658 GB
RAM usage Percent: 72.8 %
============================================================
== WITH SLOTS
memory Use: 0.1702 GB
RAM usage Percent: 71.3 %
============================================================
== WITHOUT SLOTS
memory Use: 0.2661 GB
RAM usage Percent: 72.9 %
============================================================
== WITH SLOTS
memory Use: 0.1704 GB
RAM usage Percent: 71.1 %
============================================================
댓글남기기