Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
from collections import namedtuple
from sys import getsizeof
from typing import NamedTuple
test_namedtuple = namedtuple("TEST", "a b c d f")
a = test_namedtuple(a=1,b=2,c=3,d=4,f=5)
class TestSlots():
__slots__ = ["a","b","c","d","f"]
def __init__(self):
self.a=1
self.b=2
self.c=3
self.d=3
self.f=3
class TestClass():
a=1
b=2
c=3
d=3
f=3
class TestNamedTuple(NamedTuple):
color: str
mileage: float
automatic: bool
car1 = TestNamedTuple('red', 3812.4, True)
b = TestSlots()
c = TestClass()
d = {'a':1, 'b':2, 'c':3, 'd': 4, 'f': 5}
if __name__ == '__main__':
from timeit import timeit
print("========= namedtuple =========")
print(f"Size: {getsizeof(a)}")
print("with a, b, c:")
print(timeit("z = a.c", "from __main__ import a"))
print("using index:")
print(timeit("z = a[2]", "from __main__ import a"))
print("========= Class using __slots__ =========")
print(f"Size: {getsizeof(b)}")
print("with a, b, c:")
print(timeit("z = b.c", "from __main__ import b"))
print("========= Class without using __slots__ =========")
print(f"Size: {getsizeof(c)}")
print("with a, b, c:")
print(timeit("z = c.c", "from __main__ import c"))
print("========= Dictionary =========")
print(f"Size: {getsizeof(d)}")
print("with keys a, b, c:")
print(timeit("z = d['c']", "from __main__ import d"))
print("========= NamedTuple with three values =========")
print(f"Size: {getsizeof(car1)}")
print("with keys a, b, c:")
print(timeit("z = car1.color", "from __main__ import car1"))

dbader commented Jun 11, 2017

Those are some interesting results!

========= namedtuple =========
Size: 88
with a, b, c:
0.996366665000096
using index:
0.4506532130180858
========= Class using __slots__ =========
Size: 80
with a, b, c:
0.6040405160165392
========= Class without using __slots__ =========
Size: 56
with a, b, c:
0.514406416012207
========= Dictionary =========
Size: 240
with keys a, b, c:
0.42917438098811544
========= NamedTuple =========
Size: 88
with keys a, b, c:
0.9738555750227533
========= Tuple =========
Size: 88
using index:
0.45133809398976155

Note: I added plain tuples and I changed the NamedTuple example to have the same number of attributes & the same length (a, b, c, d, f):

from collections import namedtuple
from sys import getsizeof
from typing import NamedTuple


test_namedtuple = namedtuple("TEST", "a b c d f")
a = test_namedtuple(a=1,b=2,c=3,d=4,f=5)

class TestSlots():
    __slots__ = ["a","b","c","d","f"]

    def __init__(self):
        self.a=1
        self.b=2
        self.c=3
        self.d=3
        self.f=3

class TestClass():
    a=1
    b=2
    c=3
    d=3
    f=3

class TestNamedTuple(NamedTuple):
    a: int
    b: int
    c: int
    d: int
    f: int

car1 = TestNamedTuple(1, 2, 3, 4, 5)

b = TestSlots()
c = TestClass()

d = {'a':1, 'b':2, 'c':3, 'd': 4, 'f': 5}
e = (1, 2, 3, 4, 5)


if __name__ == '__main__':
    from timeit import timeit

    print("========= namedtuple =========")
    print(f"Size: {getsizeof(a)}")
    print("with a, b, c:")
    print(timeit("z = a.c", "from __main__ import a", number=10000000))

    print("using index:")
    print(timeit("z = a[2]", "from __main__ import a", number=10000000))

    print("========= Class using __slots__ =========")
    print(f"Size: {getsizeof(b)}")
    print("with a, b, c:")
    print(timeit("z = b.c", "from __main__ import b", number=10000000))

    print("========= Class without using __slots__ =========")
    print(f"Size: {getsizeof(c)}")
    print("with a, b, c:")
    print(timeit("z = c.c", "from __main__ import c", number=10000000))

    print("========= Dictionary =========")
    print(f"Size: {getsizeof(d)}")
    print("with keys a, b, c:")
    print(timeit("z = d['c']", "from __main__ import d", number=10000000))

    print("========= NamedTuple =========")
    print(f"Size: {getsizeof(car1)}")
    print("with keys a, b, c:")
    print(timeit("z = car1.c", "from __main__ import car1", number=10000000))

    print("========= Tuple =========")
    print(f"Size: {getsizeof(e)}")
    print("using index:")
    print(timeit("z = e[2]", "from __main__ import e", number=10000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment