Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about
for i in range(0, 20):for i in xrange(0, 20):
for i in range(0, 20):
for i in xrange(0, 20):
range() and xrange() are two functions that can be used to iterate a certain number of times in a for loop in Python. xrange()is only used in, Python 2. So to run any code in both Python 2 and Python 3 you should use range() function.
range() – It will create a list of values from depending on the range specified
xrange() – It returns xrange objects that can be used to display numbers only by looping. The only a particular range is displayed on demand and hence called “lazy evaluation“.
Now the point is why xrange() is faster as compared to range(). The reason is, at the time of the memory allocation range() takes more amount of memory as compares to xrange (). The basic reason for this is the return type of range() is a list which is large in size and the return type of xrange() is xrange() object which needs less amount of memory.
An example that illustrates all the above points:-
import sysa = range(1,10000)x = xrange(1,10000)print ("The size allotted using range() is:")print (sys.getsizeof(a))print ("The size allotted using xrange() is : ")print (sys.getsizeof(x))
a = range(1,10000)
x = xrange(1,10000)
print ("The size allotted using range() is:")
print ("The size allotted using xrange() is : ")
To know more about this you can have a look at the following video tutorial:-
Certification in Digital Marketing |
Certification in Big Data Analytics |
Certification in Cloud & Devops |
Certification in Full Stack Web Development
Big Data and Data Science Master’s Course |
Data Science Architect Master’s Program |
Cloud and DevOps Architect Master’s Course |
Artificial Intelligence Engineer Master’s Course |
Microsoft Azure Certification Master’s Training
Data Science Course Online |
Big Data Hadoop Certification Training |
Machine Learning Course Online |
Artificial Intelligence Course and Training |
Salesforce Certification Training: Administrator and App Builder |
Tableau Training and Certification Course |
Python Data Science Course & Training |
Ethical Hacking Course |
Business Analyst Course |
Courses in Cyber Security
© COPYRIGHT 2011-2021 INTELLIPAAT.COM. ALL RIGHTS RESERVED.
Download Salary Trends Now !
Learn how professionals like you got up to 100% Salary Hike.