Performance Advantages to Iterators?

后端 未结 9 1754
梦毁少年i
梦毁少年i 2020-11-28 10:45

What (if any) performance advantages are offered by using iterators. It seems like the \'Right Way\' to solve many problems, but does it create faster/more memory-conscious

相关标签:
9条回答
  • 2020-11-28 11:22

    The primary benefit of iterators is not one of performance. In my experience the most performant solution is creating an algorithm which embeds your data structure of choice. The benefit of iterators is that they allow you to decouple data and algorithm and, therefore, generalize and reuse both. If this can also be done without (or with little) performance degradation then it's a net gain.

    My favorite example of iterator usage can be found in the C++ Standard Template Library. It manages to demonstrate the power and beauty of the abstraction by cleanly separating container and algorithm without sacrificing performance. Understanding this design had a profound effect on the way I think about code.

    0 讨论(0)
  • 2020-11-28 11:26

    Iterators are just classes that implement a particular interface, specifically an interface for going to the next one. In Python, lists, tuples, dicts, strings, and files all implement this interface. If they are implemented poorly, it may result in poor performance, but there is nothing inherent to the interface that implies good or bad performance.

    0 讨论(0)
  • 2020-11-28 11:28

    To backup the @Christian Witts's answer:

    range vs. xrange performance

    python25 -mtimeit "for i in xrange(1000): pass"
    10000 loops, best of 3: 56.3 usec per loop
    
    python25 -mtimeit "for i in range(1000): pass"
    10000 loops, best of 3: 80.9 usec per loop
    
    python26 -mtimeit "for i in xrange(1000): pass"
    10000 loops, best of 3: 48.8 usec per loop
    
    python26 -mtimeit "for i in range(1000): pass"
    10000 loops, best of 3: 68.6 usec per loop
    

    btw, neither range() nor xrange() are iterators:

    >>> hasattr(range(1), 'next')
    False
    >>> hasattr(xrange(1), 'next')
    False
    >>> iter(xrange(1))
    <rangeiterator object at 0x0097A500>
    >>> iter(range(1))
    <listiterator object at 0x00A7BFD0>
    >>> iter([])
    <listiterator object at 0x00A7BE30>
    >>> iter(i for i in (1,))
    <generator object at 0x00A7F940>
    >>> (i for i in (1,))
    <generator object at 0x00A7FDC8>
    
    0 讨论(0)
提交回复
热议问题