#!/usr/bin/env python # coding: utf-8 # # range() in Python 2 versus range() in Python 3 # # We run same Python code in two different Ipython/Jupyter notebooks. # The code is designed to highlight the differences in how # Python 2 and Python 3 do range(). # The function foo, iterates through range(n), # but quits the loop the first time the body of the loop is executed. # So the body of the loop takes very very little time to execute. # So long execution times for foo(n) # are mostly the execution time of range(n). # In[1]: n = 10**7 # In[2]: def foo(n): for i in range(n): break # In[3]: foo(n) # In[4]: get_ipython().run_line_magic('timeit', 'foo(n)') # In[5]: n = 10**8 # In[6]: get_ipython().run_line_magic('timeit', 'foo(n)') # In[7]: n = 10**9 # In[8]: get_ipython().run_line_magic('timeit', 'foo(n)') # In[9]: n = 10**12345 # In[10]: get_ipython().run_line_magic('timeit', 'foo(n)')