bearophileHUGS at bearophileHUGS at
Tue Nov 4 11:20:48 CET 2008

Michele Simionato:
> No, slots have nothing to do with speed, they are a memory optimization.

In many languages, often in Python too, the less memory you use/
allocate the faster you go.

In fact slots allow a speed increase too (in new style classes):

from timeit import default_timer as clock

class C1(object):
    __slots__ = ["a", "b"]
    def __init__(self, a, b):
        self.a = a
        self.a = b

class C2(object):
    def __init__(self, a, b):
        self.a = a
        self.a = b

def main(N, test):
    t0 = clock()

    if test == 1:
        [C1(ab, ab) for ab in xrange(N)]
    elif test == 2:
        [C2(ab, ab) for ab in xrange(N)]

    t1 = clock()
    print round(t1 - t0, 2)

main(N=700*1000, test=1)

Core duo 2 GHz:
test=1 ==> 1.06 s
test=2 ==> 3.0 s

(700*1000 is the way I have found to write the 700_000 I was talking
about, until we'll have a syntax for it.)


More information about the Python-list mailing list