What is the fastest (to access) struct-like object in Python?

后端 未结 6 619
旧巷少年郎
旧巷少年郎 2020-12-07 11:08

I\'m optimizing some code whose main bottleneck is running through and accessing a very large list of struct-like objects. Currently I\'m using namedtuples, for readability

6条回答
  •  执笔经年
    2020-12-07 11:18

    This question is fairly old (internet-time), so I thought I'd try duplicating your test today, both with regular CPython (2.7.6), and with pypy (2.2.1) and see how the various methods compared. (I also added in an indexed lookup for the named tuple.)

    This is a bit of a micro-benchmark, so YMMV, but pypy seemed to speed up named tuple access by a factor of 30 vs CPython (whereas dictionary access was only sped up by a factor of 3).

    from collections import namedtuple
    
    STest = namedtuple("TEST", "a b c")
    a = STest(a=1,b=2,c=3)
    
    class Test(object):
        __slots__ = ["a","b","c"]
    
        a=1
        b=2
        c=3
    
    b = Test()
    
    c = {'a':1, 'b':2, 'c':3}
    
    d = (1,2,3)
    e = [1,2,3]
    f = (1,2,3)
    g = [1,2,3]
    key = 2
    
    if __name__ == '__main__':
        from timeit import timeit
    
        print("Named tuple with a, b, c:")
        print(timeit("z = a.c", "from __main__ import a"))
    
        print("Named tuple, using index:")
        print(timeit("z = a[2]", "from __main__ import a"))
    
        print("Class using __slots__, with a, b, c:")
        print(timeit("z = b.c", "from __main__ import b"))
    
        print("Dictionary with keys a, b, c:")
        print(timeit("z = c['c']", "from __main__ import c"))
    
        print("Tuple with three values, using a constant key:")    
        print(timeit("z = d[2]", "from __main__ import d"))
    
        print("List with three values, using a constant key:")
        print(timeit("z = e[2]", "from __main__ import e"))
    
        print("Tuple with three values, using a local key:")
        print(timeit("z = d[key]", "from __main__ import d, key"))
    
        print("List with three values, using a local key:")
        print(timeit("z = e[key]", "from __main__ import e, key"))
    

    Python Results:

    Named tuple with a, b, c:
    0.124072679784
    Named tuple, using index:
    0.0447055962367
    Class using __slots__, with a, b, c:
    0.0409136944224
    Dictionary with keys a, b, c:
    0.0412045334915
    Tuple with three values, using a constant key:
    0.0449477955531
    List with three values, using a constant key:
    0.0331083467148
    Tuple with three values, using a local key:
    0.0453569025139
    List with three values, using a local key:
    0.033030056702
    

    PyPy Results:

    Named tuple with a, b, c:
    0.00444889068604
    Named tuple, using index:
    0.00265598297119
    Class using __slots__, with a, b, c:
    0.00208616256714
    Dictionary with keys a, b, c:
    0.013897895813
    Tuple with three values, using a constant key:
    0.00275301933289
    List with three values, using a constant key:
    0.002760887146
    Tuple with three values, using a local key:
    0.002769947052
    List with three values, using a local key:
    0.00278806686401
    

提交回复
热议问题