fix xrange py2

This commit is contained in:
rasbt 2014-06-16 07:47:24 -04:00
parent 3fdc0fc62c
commit fc1ccf3cde

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:53ee36d6fcce5f6dbce838e41d83e62fbb143538fe3ca1b0f3cd9b5505e5d786" "signature": "sha256:1a71ccc70829239143d02cebcb97bec031b45e676ebad340fc04c9bd4a5760bf"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -349,13 +349,13 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"Python 2.7.6\n", "Python 2.7.7\n",
"('a', 'b')\n", "('a', 'b')\n",
"a b\n" "a b\n"
] ]
} }
], ],
"prompt_number": 4 "prompt_number": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -739,7 +739,7 @@
"\n", "\n",
"n = 10000\n", "n = 10000\n",
"def test_range(n):\n", "def test_range(n):\n",
" for i in range(n):\n", " return for i in range(n):\n",
" pass\n", " pass\n",
" \n", " \n",
"def test_xrange(n):\n", "def test_xrange(n):\n",
@ -912,32 +912,26 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 4 "prompt_number": 3
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"def val_in_range(x, val):\n", "def val_in_range(x, val):\n",
" if val in range(x):\n", " return val in range(x)"
" return True\n",
" else:\n",
" return False"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 10 "prompt_number": 4
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"def val_in_xrange(x, val):\n", "def val_in_xrange(x, val):\n",
" if val in range(x):\n", " return val in xrange(x)"
" return True\n",
" else:\n",
" return False"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -961,8 +955,8 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"Python 3.3.5\n", "Python 3.4.1\n",
"1 loops, best of 3: 751 ms per loop" "1 loops, best of 3: 742 ms per loop"
] ]
}, },
{ {
@ -970,7 +964,7 @@
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"\n", "\n",
"1000000 loops, best of 3: 1.21 \u00b5s per loop" "1000000 loops, best of 3: 1.19 \u00b5s per loop"
] ]
}, },
{ {
@ -981,7 +975,7 @@
] ]
} }
], ],
"prompt_number": 11 "prompt_number": 7
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -1004,8 +998,12 @@
"print 'Python', python_version()\n", "print 'Python', python_version()\n",
"assert(val_in_xrange(x, x/2.0) == True)\n", "assert(val_in_xrange(x, x/2.0) == True)\n",
"assert(val_in_xrange(x, x/2) == True)\n", "assert(val_in_xrange(x, x/2) == True)\n",
"assert(val_in_range(x, x/2) == True)\n",
"assert(val_in_range(x, x//2) == True)\n",
"%timeit val_in_xrange(x, x/2.0)\n", "%timeit val_in_xrange(x, x/2.0)\n",
"%timeit val_in_xrange(x, x/2)" "%timeit val_in_xrange(x, x/2)\n",
"%timeit val_in_range(x, x/2.0)\n",
"%timeit val_in_range(x, x/2)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -1015,7 +1013,7 @@
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"Python 2.7.7\n", "Python 2.7.7\n",
"1 loops, best of 3: 661 ms per loop" "1 loops, best of 3: 285 ms per loop"
] ]
}, },
{ {
@ -1023,7 +1021,23 @@
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"\n", "\n",
"1 loops, best of 3: 564 ms per loop" "1 loops, best of 3: 179 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1 loops, best of 3: 658 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1 loops, best of 3: 556 ms per loop"
] ]
}, },
{ {
@ -1070,13 +1084,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 2, "prompt_number": 8,
"text": [ "text": [
"<slot wrapper '__contains__' of 'range' objects>" "<slot wrapper '__contains__' of 'range' objects>"
] ]
} }
], ],
"prompt_number": 2 "prompt_number": 8
}, },
{ {
"cell_type": "code", "cell_type": "code",