Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created June 14, 2016 10:49
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 kurozumi/d547e79ba09db17fc526fdd939264e1f to your computer and use it in GitHub Desktop.
Save kurozumi/d547e79ba09db17fc526fdd939264e1f to your computer and use it in GitHub Desktop.
【Python】Numpyで配列を足し算する
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a=np.random.random(10**7)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"b=np.random.random(10**7)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#for文で各要素を計算した場合"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 375 ms, sys: 15.8 ms, total: 390 ms\n",
"Wall time: 392 ms\n"
]
}
],
"source": [
"%time for i in range(10**6): a[i] + b[i]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#配列を足し算した場合"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 27.1 ms, sys: 24 ms, total: 51.1 ms\n",
"Wall time: 50.8 ms\n"
]
},
{
"data": {
"text/plain": [
"array([ 1.61374181, 1.15528051, 0.4731171 , ..., 1.07371708,\n",
" 0.90053945, 0.79271722])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time a + b"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment