mirror of
https://github.com/rasbt/python_reference.git
synced 2025-04-25 06:03:37 +00:00
compared python 2 and 3 speeds
This commit is contained in:
parent
667a7333cd
commit
fa0b5f185c
@ -2862,6 +2862,100 @@ In [8]:
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing text_cell rendered">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="text_cell_render border-box-sizing rendered_html">
|
||||||
|
<p><strong>Note</strong><br />Some people pointed out the speed difference between Python 3's <code>range()</code> and Python2's <code>xrange()</code>. Since they are implemented the same way one would expect the same speed. However the difference here just comes from the fact that Python 3 generally tends to run slower than Python 2.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing code_cell rendered">
|
||||||
|
<div class="input">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
In [3]:
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="input_area">
|
||||||
|
<div class="highlight"><pre><span class="k">def</span> <span class="nf">test_while</span><span class="p">():</span>
|
||||||
|
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
|
||||||
|
<span class="k">while</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">20000</span><span class="p">:</span>
|
||||||
|
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||||
|
<span class="k">return</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing code_cell rendered">
|
||||||
|
<div class="input">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
In [4]:
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="input_area">
|
||||||
|
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">'Python'</span><span class="p">,</span> <span class="n">python_version</span><span class="p">())</span>
|
||||||
|
<span class="o">%</span><span class="k">timeit</span> <span class="n">test_while</span><span class="p">()</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="output_wrapper">
|
||||||
|
<div class="output">
|
||||||
|
|
||||||
|
|
||||||
|
<div class="output_area"><div class="prompt"></div>
|
||||||
|
<div class="output_subarea output_stream output_stdout output_text">
|
||||||
|
<pre>
|
||||||
|
Python 3.4.1
|
||||||
|
100 loops, best of 3: 2.68 ms per loop
|
||||||
|
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="cell border-box-sizing code_cell rendered">
|
||||||
|
<div class="input">
|
||||||
|
<div class="prompt input_prompt">
|
||||||
|
In [6]:
|
||||||
|
</div>
|
||||||
|
<div class="inner_cell">
|
||||||
|
<div class="input_area">
|
||||||
|
<div class="highlight"><pre><span class="k">print</span> <span class="s">'Python'</span><span class="p">,</span> <span class="n">python_version</span><span class="p">()</span>
|
||||||
|
<span class="o">%</span><span class="k">timeit</span> <span class="n">test_while</span><span class="p">()</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="output_wrapper">
|
||||||
|
<div class="output">
|
||||||
|
|
||||||
|
|
||||||
|
<div class="output_area"><div class="prompt"></div>
|
||||||
|
<div class="output_subarea output_stream output_stdout output_text">
|
||||||
|
<pre>
|
||||||
|
Python 2.7.6
|
||||||
|
1000 loops, best of 3: 1.72 ms per loop
|
||||||
|
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="cell border-box-sizing text_cell rendered">
|
<div class="cell border-box-sizing text_cell rendered">
|
||||||
<div class="prompt input_prompt">
|
<div class="prompt input_prompt">
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:3442b8345ce99d8d379e475a753f23abf3392c6d4a6fdf185cf061e73e09b9e1"
|
"signature": "sha256:8b17f6649c9b74d878c198037ee831fbec78e7ac375adfc4a62f03266d9d66c3"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
@ -877,6 +877,85 @@
|
|||||||
],
|
],
|
||||||
"prompt_number": 8
|
"prompt_number": 8
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"**Note** \n",
|
||||||
|
"Some people pointed out the speed difference between Python 3's `range()` and Python2's `xrange()`. Since they are implemented the same way one would expect the same speed. However the difference here just comes from the fact that Python 3 generally tends to run slower than Python 2. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"def test_while():\n",
|
||||||
|
" i = 0\n",
|
||||||
|
" while i < 20000:\n",
|
||||||
|
" i += 1\n",
|
||||||
|
" return"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"prompt_number": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"print('Python', python_version())\n",
|
||||||
|
"%timeit test_while()"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"Python 3.4.1\n",
|
||||||
|
"100 loops, best of 3: 2.68 ms per loop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt_number": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"print 'Python', python_version()\n",
|
||||||
|
"%timeit test_while()"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"Python 2.7.6\n",
|
||||||
|
"1000 loops, best of 3: 1.72 ms per loop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt_number": 6
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user