é«éåã®ããã®Python Tips
çããããã«ã¡ã¯
ãå
æ°ã§ããï¼ç§ã¯å
æ°ã§ãã
Pythonã«ãããé«éåææ³ãæ²è¼ãã¦ã¿ã¾ãã
ç°¡åãªã³ã¼ã並ã³ã«ç´¢å¼ã®ãããªæãã§å¼ãããããªã¤ã¡ã¼ã¸ã§ä½æãã¾ããã
æ¬æ¥ã®ç®æ¬¡ã§ãã
- Pythonã«ãããé«éåã®å¿ è¦æ§
- Pythonã®é«éå
- åºæ¬çãªæ¡ä»¶
- Pythonã®åºæ¬çãªæ¸ãæ¹é¨å
- Numpyã«é¢ããTips
- ãã®ä»é«éåãã¼ã«
- ææ³ä¸¦ã³ã«å±æ
- åèæç®
Pythonã«ãããé«éåã®å¿ è¦æ§
Pythonã¯C++ãJavaã¨æ¯è¼ããã¨é常ã«é
ãè¨èªã§ãã
ããããæè¿ã¯Pythonã§æ¸ãç¨éãå¢ãã¦ãã¦ãããå人çã«ã
ä¸éçã«ãï¼å¤åï¼éè¦ãå¢ãã¤ã¤ããã¾ãã
ããè¨ç®æ©ã«è² è·ããããå¦çãæ¸ããã¨ãå¤ãã®ã§ãï¼ç§ã ãï¼ï¼ãã¤ãé度ã«æ©ã¾ããã¾ãã
ãããªããã§ãä»åã®è¨äºã§ãã
Pythonã®é«éå
é«éåã®æé
åºæ¬çã«Pythonã®é«éåã¯æ¬¡ã®æé ã§è¡ããã¾ãã
ï¼åèï¼PythonSpeed/PerformanceTips - Python Wikiï¼
- ãã¹ãï¼å®è¡ï¼
- é ããã°ãããã¡ã¤ã«åã
- æé©åãã
- ç¹°ãè¿ã
åºæ¬çã«ã¯é
ãé¨åãæå¾
éãã«åãã¦ããªãç®æãå²ãåºãããã®ç®æã«å¯¾ãã¦
対çãæã¤ã®ãåºæ¬ã§ããã§ããã®åãã¦ããªãç®æãå²ãåºãããã«ãProfilingãããä½æ¥ãããã¾ãã
Profiling
Pythonã®ã³ã¼ãã«ããããã
åºæ¬çã«ã¯Profilingãåå¾ãããã¨ããå§ãã¾ãã
ã¤ã¾ããã©ããé
ãã®ããç¹å®ããããã§ãã
å®è¡æé ã¯ç°¡åã§ããªãã·ã§ã³ã追å ãã¦å®è¡ããã°ãåæã«åå¾ã§ãã¾ãã
以ä¸ã¯ä»åå®è¡ããä¾ã§ããçç®ãã¤ã³ãã¯cumtimeãncallsã§ãã
cumtimeãé«ããã°å®è¡æéãé·ãã®ã§ããã®é¨åã«é
ãå¦çãå
¥ãã¦ããå¯è½æ§ãé«ãã§ãã
ncallsãé«ãå ´åã¯ç¡é§ã«é¢æ°ãå¼ãã§ããå¯è½æ§ãããã¾ãã
ãããã®å¯è½æ§ãè¸ã¾ããªããProfileãè¦ãã¨é«éåã®æç«ã¦ãçºè¦ã§ããããããã¾ããã
â»-s ã¯æéã§ã½ã¼ãããã¨ãã£ãæå³ã§ãã
python -m cProfile -s time import_cost.py 10012657 function calls (10012536 primitive calls) in 9.465 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 1 7.273 7.273 7.777 7.777 import_cost.py:14(list_append_import) 1 1.298 1.298 1.612 1.612 import_cost.py:6(list_append) 10001436 0.642 0.000 0.642 0.000 {method 'append' of 'list' objects} 1 0.076 0.076 9.465 9.465 import_cost.py:1(<module>) 3 0.053 0.018 0.225 0.075 __init__.py:1(<module>) 1 0.010 0.010 0.011 0.011 __init__.py:88(<module>) 1 0.009 0.009 0.013 0.013 __init__.py:15(<module>) 1 0.009 0.009 0.176 0.176 __init__.py:106(<module>) 2 0.008 0.004 0.020 0.010 __init__.py:45(<module>) 1 0.006 0.006 0.016 0.016 utils.py:4(<module>) 1 0.005 0.005 0.009 0.009 __init__.py:41(<module>) 1 0.005 0.005 0.005 0.005 npyio.py:1(<module>) 1 0.004 0.004 0.009 0.009 numeric.py:1(<module>) 1 0.004 0.004 0.007 0.007 index_tricks.py:1(<module>) 1 0.003 0.003 0.012 0.012 _internal.py:6(<module>) 1 0.003 0.003 0.004 0.004 case.py:1(<module>) 1 0.003 0.003 0.008 0.008 tempfile.py:18(<module>) 1 0.003 0.003 0.019 0.019 decorators.py:15(<module>)
åºæ¬çãªæ¡ä»¶
è¨æ¸¬ã³ã¼ã
è¨æ¸¬æéãæ¸ãã¦ããè
ã«ã¤ãã¦ã¯æ¬¡ã®ãããªæ¹å¼ã§è¡ãã¾ããã
é常ã«ããããã³ã¼ããã¨æããã¾ãã
start = time.time() #å¦ç Ex.list_append_local_val() print time.time() - start
Pythonã®åºæ¬çãªæ¸ãæ¹é¨å
rangeããxrangeãï¼Python2.7ï¼
Pythonã¯ã«ã¼ãã2éãã®è¨è¼ã®ä»æ¹ããããã¨ãã§ãã¾ãã
xrangeã¯ã¡ã¢ãªã«æããªãã®ã§ãrangeãããé«éã«ã«ã¼ããåããã¨ãã§ãã¾ãã
# é ã def sum_range(): result = 0 for i in range(100): result += i # æ©ã def sum_xrange(): result = 0 for i in xrange(100): result += i
詳ããé度æ¯è¼ã¯ãã¡ãã«è¨è¼ãã¦ãã¾ãã
ãªã¹ãã®çæ
ãªã¹ãã¯å
å
表è¨ã§çæããã®ãé«éã§ãã
å
å
表è¨ãããã¨ã¡ã¢ãªã«ä¿åããå¿
è¦ããªãããã§ãã
å®éã«ã¯æ¬¡ã®ããã«è¨è¼ãã¾ãã
# é常ã®æ¸ãæ¹ list = [] for i in xrange(1000000): list.append(i) # ãªã¹ãå å è¡¨è¨ list = [i for i in xrange(1000000)]
æååçµå
joinã使ãã°ç°¡åã«çµåãããã¨ãã§ãããªããã¤é«éãªã³ã¼ããè¨è¿°ãããã¨ãã§ãã¾ãã
def string_operator_join(join_words): denominator = "" words = "" for word in join_words: words = denominator + word denominator = "," return words def string_join(join_words): return ",".join(join_words)
Importæã®ã³ã¹ã
Pythonã¯importæãé¢æ°ã®ä¸ã«æ¸ããã¨ãã§ãã¾ãã
ããããimportãããã³ã¹ããå¿
è¦ã¨ãªãã®ã§ãimportãé¢æ°å
ã§è¨è¼ããæã«ã¯æ³¨æãå¿
è¦ã§ãã
ä¾ãã°ã以ä¸ã®ãããªé¢æ°ãï¼ã¤ç¨æãã¾ãããéãã¯é¢æ°ã®éä¸ã«import numpy as npã追å ãã¦ãããã©ããã§ãã
N = 10000000 def list_append(): result = [] for i in xrange(N): if i % 2 == 0: result.append(i) return result def list_append_import(): result = [] for i in xrange(N): import numpy as np #numpyãä½åº¦ãimportããã if i % 2 == 0: result.append(i) return result
ä¸è¨ãæ¯è¼ããã¨æ¬¡ã®ããã«ãªãã¾ãã
é¢æ°å | ããã£ãæé(s) |
list_append | 1.25 |
list_append_import | 7.43 |
é¢æ°å¼ã³åºãã®ã³ã¹ã
é¢æ°ãå¼ã³åºãã®ã«ãã³ã¹ãããããã¾ãããã®ããããã¾ãã«ã³ã¹ããé«ãå¼ã³åºãã¯é¿ãã¾ãããã
追å ããªãããªã¹ããçæããlist_appendé¢æ°ãå¤æ´ãã¦ã¿ã¾ããã
def append_number(i, number_list): if i % 2 == 0: number_list.append(i) def list_append_local_val(): result = [] for i in xrange(N): append_number(i, result)
é¢æ°å | ããã£ãæéï¼sï¼ |
list_append | 1.48743391037 |
list_append_local_val | 2.39971590042 |
ããããé¿ãã
ããããé¿ãããã¨ã§ãè¥å¹²ã®ã¹ãã¼ãåä¸ããããã¨ãã§ãã¾ãã
å
·ä½çãªãããªã³ã¼ãã«ãªãã¾ãã
N = 10000000 def list_append_local_val(): result = [] append = result.append for i in xrange(N): if i % 2 == 0: append(i) return result
yieldã使ã
æå¾ã«yieldã使ãã¾ãã
å
ã
C++ã使ã£ãå®è£
ãã¡ã¤ã³ã¨ãã¦ããã®ã§
使ãæ©ä¼ã¨ãããã馴æã¿ããªãã£ãã®ã§ããã使ã£ã¦ã¿ãã¨ã¨ã¦ã使ããããã
æã¯ãã¾ãã«é¦´æã¿ããªãã®ã§å©ç¨ãã¦ãã¾ããã§ãããã
Deep Learningã®ã¢ã«ã´ãªãºã ã®Real Time Data Augmentationãä¸å®ã®å¦çãå®æ½ããæã«ç¹ã«ä½¿ãã¾ãã
â ã¡ã¢ãªãæ¶èããªã
yieldã®ã¡ãªããã¯ãå¦çãä¸å®ã®ééï¼yieldï¼ã§å
ã®é¢æ°ã§è¿ãã®ã§ãã¡ã¢ãªãæ¶è²»ãã«ãã
â¡ç´æçã«ã«ã¼ããæ¸ããã
Pythonã ã¨ãã«ã¼ãã®ä¸ã§yieldãå¼ã³åºãå¦çãæ¸ãã ããªã®ã§é常ã«ããããããã使ããããã
ï¼ããããããªãã°ãã£ã¨å¥ã®æ¸ãæ¹ãããã§ããã»ã»ã»ï¼
#yield ãªã list = [i * 2 for i in xrange(100)] for i in xrange(list): print i + 10 #yield ãã def iterate(number): for i in xrange(number): yield i * 2 for j in iterate(100): print j + 10
Numpyã«é¢ããTips
Numpyã¯ããã¤ãé«éå並ã³ã«æ³¨æããç¹ãããã¾ãã
Numpyã使ç¨ãã¦åºæ¬æ¼ç®ãé«éåãã
Numpyã¯åºæ¬çã«Cã§è¨è¿°ãããå®è¡é度ãé常ã«é«éã§ãã
ãã®ãããNumpyã§æ¸ããã¨ããã¯Numpyã§æ¸ãã¨ãé«éã«ãªãè¨ç®ã¨ãªãã¾ãã
ä¾ãã°ãåè¨å¤ãæ±ããæ¼ç®ã¯æ¬¡ã®ããã«ãªãã¾ãã
# é常ã®ãªã¹ãã§ã®æ¸ãæ¹ def number_element_sum(number_list): result = 0.0 for i in xrange(len(number_list)): result += number_list[i] return result # Numpyã使ãã®ã§ãé«éã«æ¸ããã def numpy_sum(numpy_list): return np.sum(numpy_list)
Numpyã®è¦ç´ ã«ã¢ã¯ã»ã¹ããæ¼ç®ãããªã
Numpyã¯é常ã®ãªã¹ãæ§é ã¨åãããã¢ã¯ã»ã¹ã¯ã§ãã¾ãã
ããããã¢ã¯ã»ã¹ããé度ãç°æ§ã«é
ãããã極åã¢ã¯ã»ã¹ã¯é»æ¢ããnumpyã使ã£ã¦æ¼ç®ããããã«ãã¾ãã
import numpy as np # ã¢ã¯ã»ã¹ãç¹°ãè¿ãã®ã§é常ã«ä½é def numpy_element_sum(numpy_list): result = 0.0 for i in xrange(len(numpy_list)): result += numpy_list[i] return result #ãã¡ããé«é def numpy_sum(numpy_list): return np.sum(numpy_list)
詳ããã¯éå»ã®è¨äºãåèã«ãã¦ãã ããã
Numbaã§ææ©ãé«éå
Numbaã使ãã¨ã¢ããã¼ã·ã§ã³ã使ãã®ã¿ã§ã³ã¼ããé«éåã§ãã¾ãã
以ä¸ã®ã³ã¼ããç¨ãã¦ãç§ã®å®è¡ç°å¢ã§æ¤è¨¼ãã¦ã¿ã¾ããã
yutori-datascience.hatenablog.com
numbaãé©ç¨ãããã¡ã½ããã«@jitï¼ããã±ã¼ã¸ã¯numbaï¼ãä»ä¸ãããã¨ã§
åæã«é©ç¨ããããããã®ã§ãã詳ãã使ãæ¹ã¯å¥éã試ãã¦ãããã¨æãã¾ãã
python: 3.78625607491 numba: 0.206073999405
ãã®ä»é«éåãã¼ã«
Cython
Cythonã¯ãPythonã§æ¸ãããã³ã¼ããCãC++ã®ããã«ã³ã³ãã¤ã«ãããã¨ã§é«éåãããã¼ã«ã§ãã
詳ããã¯ä»¥ä¸ã®è¨äºã御覧ãã ããã
Dask
Daskã¯æè»ãªä¸¦ååæ£ã©ã¤ãã©ãªã§ãã
ã·ã³ãã«ã«è¨è¿°ã§ãã¦ãé常ã«è¯ãæãã§ãã
ããããããç¨åº¦å¤§è¦æ¨¡ã«ãªããªãã¨ãé«éåããªãããã«æããããã®ã§
ããç¨åº¦å¯¾å¿ããè¦æ¨¡ãè¦ç©ããå¿
è¦ãããã¨æãã¾ãã
PyPy
Pythonãé«éã«å®è£
ããã®ãPyPyã§ããä½ããç°¡åã«å®è¡ãã§ãã代ããã«
numpy, scipyå¨ããåããªããããªãã®ãåºã¾ããï¼ãã®ãã使ã£ã¦ããã¾ããã»ã»ï¼
詳ããã¯ä»¥ä¸ãã覧ãã ããã
nonbiri-tereka.hatenablog.com
å
¬å¼ãã¼ã¸
pypy.org
ææ³ä¸¦ã³ã«å±æ
ä»åã¯Pythonã«ãããé«éåã¨ãã£ã観ç¹ã§è¨äºãæ¸ãã¾ããã
ãã£ã¨ãããªä¾¿å©ãªã®ãããã¾ãã¨ãã£ã話ãããã°ããã²ã