Comments (3)
Thanks for the comment! The python 2's xrange
does have a __contain__
attribute (yet) it was added to Python 3's range
though. However, I can't confirm any speed improvements here (but that might be more due to the fact that Py3.4 is generally slower.
x = 10000000
val = x/2
def val_in_xrange(x, val):
if val in xrange(x):
pass
def val_in_range(x, val):
if val in range(x):
pass
def val_in_range_workaround(x, val):
if 0 <= val <= x-1:
pass
print 'Python', python_version()
%timeit val_in_xrange(x, val) # since not supported in Py 3.x
%timeit val_in_range(x, val)
%timeit val_in_range_workaround(x, val)
Python 2.7.7
10 loops, best of 3: 181 ms per loop
1 loops, best of 3: 557 ms per loop
1000000 loops, best of 3: 420 ns per loop
print('Python', python_version())
# %timeit val_in_xrange(x, val) # since not supported in Py 3.x
%timeit val_in_range(x, val)
%timeit val_in_range_workaround(x, val)
Python 3.4.1
1 loops, best of 3: 740 ms per loop
1000000 loops, best of 3: 545 ns per loop
from python_reference.
I checked the source code about range.__contains__
in Python3, and turns out this method will have a fallback faster implementation if the value to be looked up is an integer or a boolean.
For example, the following code in Python3:
i = 100000000
%timeit i/2 in range(i)
1 loops, best of 3: 5.64 s per loop
%timeit i//2 in range(i)
1000000 loops, best of 3: 765 ns per loop
from python_reference.
Awesome, thanks a lot for pointing this out! I really appreciate it - I love to learn new things about Python that I didn't know before!
I added it to the IPython notebook and mentioned you if you don't mind: http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb?create=1#xrange
from python_reference.
Related Issues (16)
- Wrong iteration test HOT 1
- Slight lambda-in-closures issue HOT 3
- Workaround for HTML Conversion in Table of Contents HOT 1
- Sentence in Example 3.1 "Here is Why" ends prematurely HOT 1
- Modifying a list while looping through it HOT 1
- old
- Broken link in SQLite3_howto README HOT 1
- Printing database summary HOT 3
- Bad generated html HOT 1
- Sorting Algorithms IPython nb link is bad. HOT 1
- python_reference
- python-reference
- Use header cell. HOT 3
- %watermark addition. HOT 4
- PyBuilder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python_reference.