Skip to content

Instantly share code, notes, and snippets.

@lgw4
Last active December 17, 2015 22:19
Show Gist options
  • Save lgw4/5681213 to your computer and use it in GitHub Desktop.
Save lgw4/5681213 to your computer and use it in GitHub Desktop.
range = getattr(__builtins__, 'xrange', range)
In [1]: import timeit
In [2]: timeit.timeit ('exes = [x for x in range(1000000)]', number=1000)
Out[2]: 62.725528955459595
In [3]: timeit.timeit ('exes = [x for x in xrange(1000000)]', number=1000)
Out[3]: 48.693825006484985
In [4]: range = getattr(__builtins__, 'xrange', range)
In [5]: timeit.timeit ('exes = [x for x in range(1000000)]', number=1000)
Out[5]: 62.58231282234192
In [6]: timeit.timeit ('exes = [x for x in range(1000000)]', number=1000, setup="from __main__ import range")
Out[6]: 48.48969316482544
In [7]: range == xrange
Out[7]: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment