returning value from list vs dictionary

前端 未结 4 648
我在风中等你
我在风中等你 2021-01-19 15:56

I\'m making a chess engine and for my piece square tables i can use lists or dictionaries. As the implementation of the piece square tables made the engine two times slower,

相关标签:
4条回答
  • 2021-01-19 16:42

    As you can see in the python wiki on TimeComplexity, list and dict they both have the same complexity in the average case on getting an item of O(1). So there should be not that much of a difference for simple index based look-ups.

    EDIT: I just wrote a little benchmarking code that gets the first element, one from the center and the last. You see that a list has a small advance (although a deviation of 0.01s is not that large when considering that the code runs 1000000 times).

    In summary I would use a list if I were in your situation, as it also fits better to the problem of an index based request.

    >>> from timeit import Timer
    >>> t=Timer("(l[0], l[3], l[7])","l=[50, 30, 30, 30, 20, 30, 50, 40]")
    >>> sorted(t.repeat(5))
    [0.17861513267149576, 0.17863279532627985, 0.17883092423682, 0.17892576501373014, 0.18901037296996037]
    >>> t=Timer("(l[0], l[3], l[7])","l={0: 50, 1: 30, 2: 30, 3: 30, 4: 20, 5: 30, 6: 50, 7: 40}")
    >>> sorted(t.repeat(5))
    [0.18541179903735383, 0.1855488765975224, 0.1855757545505412, 0.18578041096390052, 0.21753940019925722]
    
    0 讨论(0)
  • 2021-01-19 16:43

    You can easily benchmark this with the timeit module (python -m timeit -S "setup" -- "statement"). But in this case, I can tell you dictionaries won't beat lists, because list lookup is trivial in comparison to dict lookup. Both will be fast, independent from the key and the number of items in the collection, but list will win on micro benchmarks. Of course, this shouldn't rule your decision. Go for clarity, optimize algorithms instead of insignificant details, premature optimization, etc. ad nauseam.

    But I'd still say a list is more appropriate, both semantically (you don't have a free-form mapping, you have a sequence), and for preventing errors (by rejecting out of range and non-integer indices).

    0 讨论(0)
  • 2021-01-19 16:44

    You are better off using a list than a dict in terms of lookup speed. Tuple is even a bit faster.

    timeit dict_ex[2]
    10000000 loops, best of 3: 148 ns per loop
    
    timeit list_ex[2]
    10000000 loops, best of 3: 116 ns per loop
    
    tuple_ex=tuple(list_ex)
    
    timeit tuple_ex[2]
    10000000 loops, best of 3: 112 ns per loop
    
    0 讨论(0)
  • 2021-01-19 16:47

    Python in general is going to be slow for something like this.

    Take a look at this other SO question, the first answer.

    I'd say your best bet here is tuples first if your object can be immutable. If not, then stick with lists. Either way, there isn't going to be a huge difference between list and dict.

    I see that halex has also just answered, and seems to agree.

    0 讨论(0)
提交回复
热议问题