-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslots_testing.py
61 lines (39 loc) · 925 Bytes
/
slots_testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""Usage of slots"""
from time import time
import sys
class Parent:
__slots__ = (
"a"
)
def __init__(self, a):
self.a = a
class WithoutSlots(Parent):
def __init__(self, a, b):
super().__init__(a)
self.b = b
class WithSlots(Parent):
__slots__ = (
"b",
)
def __init__(self, a, b):
super().__init__(a)
self.b = b
N = 10 ** 7
with_s = time()
for j in range(N):
temp = WithSlots(j, j)
tot_no_s = time() - with_s
print(tot_no_s)
no_s = time()
for j in range(N):
temp = WithoutSlots(j, j)
tot_s = time() - no_s
print(tot_s)
print(1 - tot_no_s / tot_s)
# Simple size difference
no_slots = WithoutSlots(1, 2)
no_slots_size = sys.getsizeof(no_slots) + sys.getsizeof(no_slots.__dict__)
with_slots = WithSlots(1, 2)
with_slots_site = sys.getsizeof(with_slots)
print(no_slots_size - with_slots_site)