numpy test

This commit is contained in:
rasbt 2014-05-01 16:07:40 -04:00
parent 191ff49ab7
commit eb35644f7b
2 changed files with 226 additions and 4 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:ab74fdc9e8ae58388d1fc428599abc86a3d03938d0f51769cef38ab4028d516a"
"signature": "sha256:d5895f75b2ac58db150d7b521682366a447ffb2fb0b7db7e551edd40e6d1ab10"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -66,7 +66,8 @@
" - [Adding elements to a dictionary](#adding_dict_elements)\n",
"- [Comprehensions vs. for-loops](#comprehensions)\n",
"- [Copying files by searching directory trees](#find_copy)\n",
"- [Returning column vectors slicing through a numpy array](#row_vectors)"
"- [Returning column vectors slicing through a numpy array](#row_vectors)\n",
"- [Speed of numpy functions vs Python built-ins and std. lib.](#numpy)"
]
},
{
@ -1691,6 +1692,116 @@
],
"prompt_number": 91
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='numpy'></a>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Speed of numpy functions vs Python built-ins and std. lib."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import timeit\n",
"\n",
"samples = list(range(1000000))\n",
"\n",
"%timeit(sum(samples))\n",
"%timeit(np.sum(samples))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 loops, best of 3: 18.3 ms per loop\n",
"10 loops, best of 3: 136 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit(list(range(1000000)))\n",
"%timeit(np.arange(1000000))\n",
"\n",
"# note that in Python range() is implemented as xrange()\n",
"# with lazy evaluation (generator)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 82.6 ms per loop\n",
"100 loops, best of 3: 5.35 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import statistics\n",
"\n",
"%timeit(statistics.mean(samples))\n",
"%timeit(np.mean(samples))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 1.14 s per loop\n",
"10 loops, best of 3: 141 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:ab74fdc9e8ae58388d1fc428599abc86a3d03938d0f51769cef38ab4028d516a"
"signature": "sha256:d5895f75b2ac58db150d7b521682366a447ffb2fb0b7db7e551edd40e6d1ab10"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -66,7 +66,8 @@
" - [Adding elements to a dictionary](#adding_dict_elements)\n",
"- [Comprehensions vs. for-loops](#comprehensions)\n",
"- [Copying files by searching directory trees](#find_copy)\n",
"- [Returning column vectors slicing through a numpy array](#row_vectors)"
"- [Returning column vectors slicing through a numpy array](#row_vectors)\n",
"- [Speed of numpy functions vs Python built-ins and std. lib.](#numpy)"
]
},
{
@ -1691,6 +1692,116 @@
],
"prompt_number": 91
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='numpy'></a>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Speed of numpy functions vs Python built-ins and std. lib."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import timeit\n",
"\n",
"samples = list(range(1000000))\n",
"\n",
"%timeit(sum(samples))\n",
"%timeit(np.sum(samples))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 loops, best of 3: 18.3 ms per loop\n",
"10 loops, best of 3: 136 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit(list(range(1000000)))\n",
"%timeit(np.arange(1000000))\n",
"\n",
"# note that in Python range() is implemented as xrange()\n",
"# with lazy evaluation (generator)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 82.6 ms per loop\n",
"100 loops, best of 3: 5.35 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import statistics\n",
"\n",
"%timeit(statistics.mean(samples))\n",
"%timeit(np.mean(samples))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 1.14 s per loop\n",
"10 loops, best of 3: 141 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,