-3

I have the following task:

write a Python2 function which prints the time the Python interpreter needs to loop through range from 0 to 4000000000, in a for cycle with an empty body

How can I solve this, my sys.maxint is only 2147483647 ?

>>> range(4000000000)
OverflowError: range() result has too many items

>>> xrange(4000000000)
OverflowError: Python int too large to convert C long

I use Windows 7 64 bit and python 2.7.10

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
DavidS
  • 276
  • 1
  • 17

5 Answers5

2

You can use a generator function with a while loop:

def my_range(n):
    i = 0
    while i < n:
        yield i
        i += 1   # will cast to long at some point, but still work
    return

for x in my_range(4000000000):
    pass

Update: solution with xrange and negative integers does not work!

user2390182
  • 72,016
  • 6
  • 67
  • 89
2

Use range() with a large number in Python 2 is a bad idea. I don't care about sys.maxint. It will simply over head your pc memory.

xrange() is a better solution. But xrange is implemented in C and requires its arguments to fit into a C long.

Range of C long is: -2^31+1 to +2^31-1 (-2147483647 to 2147483647)

4000000000  - 2147483647 = 1852516353

You can use itertools.count() like the answer of Martijn here . It does the same like xrange, but is in Python implemented. count() return a infinity iterator . So you need islice to stop it.

from itertools import islice, count
for i in islice(count(0), 4000000000):
    print i
Community
  • 1
  • 1
qvpham
  • 1,896
  • 9
  • 17
1

I think the task was meant to make you understand what xrange does and what iterators are. From the documentation:

This function is very similar to range(), but returns an xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with break). For more information on xrange objects, see XRange Type and Sequence Types str, unicode, list, tuple, bytearray, buffer, xrange.

Practically speaking, try writing the for loop instead of using it directly:

for i in range(.....):
    pass

Versus:

for i in xrange(.....):
    pass

And see what happens.

lorenzog
  • 3,483
  • 4
  • 29
  • 50
0

If you can use nested for loops, something like this might work:

for i in xrange(200000):
    for j in xrange(20000):
        pass
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

As you can see, in Python 2, neither range nor xrange can yield sequences that are longer than sys.maxint (which is the system's long int maximum).

So, the only way to complete your assignment "to a 't'" is to run it in 64-bit Python.

All other ways (nested loops, custom iterators, more than one loop, etc) are different workloads and will give different times. All you can do is get some approximation to how long a "genuine" loop would take. (E.g. calculate how long, on average, it takes for the interpreter to "set up a loop", then run a few loops with this total count and subtract extra setup times)

So, you have to consult your tutor (or whoever gave you the task) and confirm if the workarounds are an acceptable solution. Or maybe they will simply allow you to use a smaller number.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152