Last active
June 3, 2018 15:54
-
-
Save polyvertex/c1b167f06bc010dc83042b3cc116a7a9 to your computer and use it in GitHub Desktop.
Small performance test on class objects creation versus named tuples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
from pprint import pprint | |
import sys | |
import timeit | |
class C1: | |
__slots__ = ('a', ) | |
class C2: | |
__slots__ = ('a', ) | |
def __init__(self): | |
pass | |
class C3: | |
__slots__ = ('a', ) | |
def __init__(self, a): | |
self.a = a | |
class C4: | |
def __init__(self, a): | |
self.a = a | |
class C5: | |
__slots__ = ( | |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l') | |
def __init__(self, a, b, c, d, e, f, g, h, i, j, k, l): | |
self.a = a | |
self.b = b | |
self.c = c | |
self.d = d | |
self.e = e | |
self.f = f | |
self.g = g | |
self.h = h | |
self.i = i | |
self.j = j | |
self.k = k | |
self.l = l | |
class C6: | |
__slots__ = ( | |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') | |
def __init__(self, a, b, c, d, e, f, g, h, i, j, k, l, m, n, | |
o, p, q, r, s, t, u, v, w, x, y, z): | |
self.a = a | |
self.b = b | |
self.c = c | |
self.d = d | |
self.e = e | |
self.f = f | |
self.g = g | |
self.h = h | |
self.i = i | |
self.j = j | |
self.k = k | |
self.l = l | |
self.m = m | |
self.n = n | |
self.o = o | |
self.p = p | |
self.q = q | |
self.r = r | |
self.s = s | |
self.t = t | |
self.u = u | |
self.v = v | |
self.w = w | |
self.x = x | |
self.y = y | |
self.z = z | |
N1 = namedtuple('N1', ('a', )) | |
N2 = namedtuple('N2', ( | |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l')) | |
N3 = namedtuple('N2', ( | |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')) | |
pprint(( | |
('C1', sys.getsizeof(C1())), | |
('C2', sys.getsizeof(C2())), | |
('C3', sys.getsizeof(C3(1))), | |
('C4', sys.getsizeof(C4(1))), | |
('C5', sys.getsizeof(C5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))), | |
('C6', sys.getsizeof(C6(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))), | |
('N1', sys.getsizeof(N1(1))), | |
('N2', sys.getsizeof(N2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))), | |
('N3', sys.getsizeof(N3(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))))) | |
loops = 10_000_000 | |
pprint(('C1', timeit.timeit(stmt='C1()', setup="from __main__ import C1", number=loops))) | |
pprint(('C2', timeit.timeit(stmt='C2()', setup="from __main__ import C2", number=loops))) | |
pprint(('C3', timeit.timeit(stmt='C3(1)', setup="from __main__ import C3", number=loops))) | |
pprint(('C4', timeit.timeit(stmt='C4(1)', setup="from __main__ import C4", number=loops))) | |
pprint(('C5a', timeit.timeit(stmt='C5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)', setup="from __main__ import C5", number=loops))) | |
pprint(('C5kw', timeit.timeit(stmt='C5(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12)', setup="from __main__ import C5", number=loops))) | |
pprint(('C6a', timeit.timeit(stmt='C6(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)', setup="from __main__ import C6", number=loops))) | |
pprint(('C6kw', timeit.timeit(stmt='C6(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12, m=13, n=14, o=15, p=16, q=17, r=18, s=19, t=20, u=21, v=22, w=23, x=24, y=25, z=26)', setup="from __main__ import C6", number=loops))) | |
pprint(('N1', timeit.timeit(stmt='N1(a=1)', setup="from __main__ import N1", number=loops))) | |
pprint(('N2a', timeit.timeit(stmt='N2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)', setup="from __main__ import N2", number=loops))) | |
pprint(('N2kw', timeit.timeit(stmt='N2(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12)', setup="from __main__ import N2", number=loops))) | |
pprint(('N3a', timeit.timeit(stmt='N3(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)', setup="from __main__ import N3", number=loops))) | |
pprint(('N3kw', timeit.timeit(stmt='N3(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12, m=13, n=14, o=15, p=16, q=17, r=18, s=19, t=20, u=21, v=22, w=23, x=24, y=25, z=26)', setup="from __main__ import N3", number=loops))) | |
# Output (CPython 3.6.5 x64 on Windows 8.1): | |
# (('C1', 48), | |
# ('C2', 48), | |
# ('C3', 48), | |
# ('C4', 56), | |
# ('C5', 136), | |
# ('C6', 248), | |
# ('N1', 56), | |
# ('N2', 144), | |
# ('N3', 256)) | |
# ('C1', 0.509869026157978) | |
# ('C2', 1.1899675120284186) | |
# ('C3', 1.614349688712295) | |
# ('C4', 1.8756573730015655) | |
# ('C5a', 4.753466218232507) | |
# ('C5kw', 10.637675061010755) | |
# ('C6a', 9.046176812642816) | |
# ('C6kw', 20.613434271897447) | |
# ('N1', 3.416388183739457) | |
# ('N2a', 3.9588902991877504) | |
# ('N2kw', 9.459237508635518) | |
# ('N3a', 5.912197909896207) | |
# ('N3kw', 17.366542805636286) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment