PEP 3106 suggests slower way? Why?

前端 未结 1 966
旧巷少年郎
旧巷少年郎 2021-01-18 23:24

Recently, I had to convert the values of a dictionary to a list in Python 3.6 and an use case where this is supposed to happen a lot.
Trying to be a good guy I wanted to

相关标签:
1条回答
  • 2021-01-18 23:42

    The answer is because the faster syntax was first introduced in PEP 448 in 2013, while PEP 3106 you reference was written in 2006, so even if is faster in a real way now, it didn't exist when the PEP was written.

    As noted by others, the role of PEPs is not to provide a template for the fastest possible code - in general, code in PEPs will aim to be simpler and as clear as possible, because examples are generally about understanding concepts rather than achieving the best possible results, so even if the syntax did exist at the time, and is faster in a real (and reliable) way, it still may not have been used.

    A bit of further testing with larger values:

    python -m timeit -s "x = [1]*10000" "[*x]"                
    10000 loops, best of 3: 44.6 usec per loop
    
    python -m timeit -s "x = [1]*10000" "list(x)" 
    10000 loops, best of 3: 44.8 usec per loop
    

    Shows the difference isn't really two times, but rather a flat cost - I would guess it's the cost of looking up the list() built-in function. This is negligible in most real cases.

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