Iterators in Python ramble on _python

Source: Internet
Author: User

The problem arises when a loop is made in Python, and the familiar Python knows that it does not have a for loop in other languages and can only iterate through for. The most typical application is to use the range function to produce a list, and then operate with for in, as follows:

Copy Code code as follows:

#!/usr/bin/env python
For I in range (10):
Print I

The meaning of the code is well understood, range produces a list that is traversed using the for-in-most list, with the same effect as for (i = 0;i<n;i++), and the range function can be seen here. Here's the problem again, range this object will produce a list, then the contents of this list is guaranteed to be stored in memory, when the need for the number of loops is too large, is quite memory, in order to count the use of range memory, I made 6 use, respectively, to produce 100, 10000,100000,1000000,10000000,100000000 the length of the list, and then counts the memory footprint:

Copy Code code as follows:

Test code consumes memory
Range (2.0MB)
Range (10000) 2.2MB
Range (100000) 3.8MB
Range (1000000) 19.5MB
Range (10000000) 168.5MB
Range (100000000) 1465.8MB

As you can see, with the increase in the base, occupy the memory is increased geometrically, obviously in the cycle of operation, to avoid using range.

To solve this problem, Python provides another function xrange, which is very similar to range, but takes up a lot less memory than range, and the relevant instructions can be viewed here, tested, with xrange objects, regardless of the parameters, Almost none of the memory footprint has changed. The problem again, xrange interior is how to achieve, why and range performance difference so big? To verify my conjecture, first try using Python to implement a function similar to xrange zrange:

Copy Code code as follows:

#!/usr/bin/env Python
Class Zrange (object):
    def __init__ (self,stop):
        self.__pointer=0
         self.stop=stop
    def __iter__ (self): 
    & nbsp;   return self 
    def next (self): #python3.0, switch to __next__
&NBSP;&NBSP;&NB sp;     if self.__pointer  >= self.stop:
             raise Stopiteration
        else:
  & nbsp;         Self.__pointer = self.__pointer + 1
             return self.__pointer-1
Test = Zrange (10000000)
to I in test :
    Print i

The results of the run, like Xrange, zrange memory footprint tests and found that, like xrange, the size of the parameters had little effect on memory footprint. So what is the difference between it and range?

As mentioned earlier, range produces a list, and whether it's a custom zrange or a system-built xrange produces an object, like Xrange or Zrange, an object that is called an iterative object that provides an external traversal of its internal elements, Without being concerned with the way it is implemented internally. In the implementation of the above Zrange, the most critical implementation is to establish an internal pointer __pointer, which records the current access location, the next access can be through the state of the pointer to operate accordingly.

In Python or other languages, there are many similar ways of accessing object content in an iterative fashion, such as reading a file:

Copy Code code as follows:

#!/usr/bin/env python
f = open (' zrange.py ', ' R ')
While True:
line = F.readline ()
If not line:
Break
Print Line.strip ()
F.close ()

We all know that using ReadLine to save resources than Reandlines, in fact, ReadLine and ReadLines is similar to xrange and range, one is to record the current position through the pointer, the next visit to move the pointer forward a unit, The other is to store all the content directly into memory. In a file action function, you can also manually adjust the position of the pointer to achieve the purpose of skipping or repeatedly reading some content.

It can be said that the implementation of the iterator, its internal pointer is to save resources, so that the key to the normal operation of the iteration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.