Giter Club home page Giter Club logo

Comments (3)

rasbt avatar rasbt commented on May 23, 2024

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.

yegle avatar yegle commented on May 23, 2024

@rasbt

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.

rasbt avatar rasbt commented on May 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.