Skip to content

Instantly share code, notes, and snippets.

@szdr
Created January 3, 2017 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szdr/855c4e27e2541a74701dea9f63af347e to your computer and use it in GitHub Desktop.
Save szdr/855c4e27e2541a74701dea9f63af347e to your computer and use it in GitHub Desktop.
built-in sum vs numpy.sum
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"N = 10000000\n",
"python_list = [i for i in range(N)]\n",
"numpy_array = np.arange(N)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 loops, best of 3: 82.6 ms per loop\n"
]
}
],
"source": [
"# python sum -> python list\n",
"%timeit sum(python_list)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 959 ms per loop\n"
]
}
],
"source": [
"# python sum -> numpy array\n",
"%timeit sum(numpy_array)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loops, best of 3: 588 ms per loop\n"
]
}
],
"source": [
"# numpy sum -> python list\n",
"%timeit np.sum(python_list)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 7.46 ms per loop\n"
]
}
],
"source": [
"# numpy sum -> numpy array\n",
"%timeit np.sum(numpy_array)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment