Pythonã§multiprocessing.Poolã使ã£ã¦ãæ軽並åå¦ç
2ã³ã¢4ã¹ã¬ããCPUã®ãã·ã³ã§Pythonã使ã£ã¦ãã¨CPU使ç¨çã25%ã¾ã§ãããªããªãã¦ãããã100%ã¾ã§ä½¿ããã°ãã£ã¨é«éåã§ããã®ã§ã¯ãªããã¨æã£ã¦ããã
CPU使ç¨ç100%ã®ããã«ã¯multiprocessingã¢ã¸ã¥ã¼ã«ã使ã£ã¦ä¸¦åå¦çããã°è¯ãã
並åå¦çã¨ããã°ããã»ã¹éã§å ±æãããªã½ã¼ã¹ã®æä»å¶å¾¡ã¨ããèããªãããããªãã£ããé¢åãªå°è±¡ãããããã¡ããmultiprocessingã¢ã¸ã¥ã¼ã«ã«ã¯èªåã§ããã»ã¹ãç«ã¡ä¸ãã¦å ±æã¡ã¢ãªã管çããæ©è½ããããã©ãããã¯ãæ軽ãããªããè¨è¨ä¸ã好ã¾ãããªãã®ã§ããã§ã¯èª¬æããªãã
ããã§ã¯ãã£ã¨ç°¡åãªå ´åãèãã¦ãä»ã®ããã»ã¹ã¨ã¯ç¬ç«ã«åä½ããé¢æ°ã«å¯¾ãã¦Poolã使ã£ã¦ã¿ããPool()ã®å¼æ°ã¯ä½¿ãCPUã®æ°ã§ãå¼æ°ãªãã ã¨èªåçã«æ大æ°ã«è¨å®ãã¦ãããï¼åã®ç°å¢ã ã¨4ï¼ãããã¤ãã¡ã½ããããããã©ãçµã¿è¾¼ã¿é¢æ°ã®map()ã¨ã»ã¼åãããã«ä½¿ããæ©è½ãç¹ã«ä¾¿å©ã
ä¾ï¼
x*xãè¿ãã®ã«1ç§ãããslowf(x)ã«å¯¾ãã¦ãslowf(0)+slowf(1)+...+slowf(9)ãæ±ããã
ã½ã¼ã¹ã³ã¼ã
from multiprocessing import Pool, cpu_count, current_process import time def slowf(x): print(current_process().name, ': This started at %s.' % time.ctime().split()[3]) time.sleep(1) return x*x if __name__ == '__main__': print('cpu : %d' % 1) st = time.time() print('answer : %d' % sum(map(slowf, range(10)))) print('time : %.3f s' % (time.time()-st)) print('\ncpu : %d' % cpu_count()) st = time.time() p = Pool() print('answer : %d' % sum(p.map(slowf, range(10)))) print('time : %.3f s' % (time.time()-st))
Windowsã®å ´åãPool.mapã§ä½¿ãé¢æ°ã¯mainã®å¤ã§å®ç¾©ãã¦mainã®ä¸ã§å¼ã¶å¿
è¦ãããã
â»ã(Windows ç°å¢ã§) if __name__ == '__main__' ã¨ããè¨è¿°ãå¿
è¦ãªçç±ã«ã¤ãã¦ã¯ã
17.2. multiprocessing — プロセスベースの並列処理 — Python 3.3.3 ドキュメント
ãåç
§ãã¦ãã ããã
çµæ
並åå¦çãªãã ã¨åslowf()ãä¸ç§ãããã®ã10åå¼ã³åºãã®ã§å½ç¶10ç§ãããã
ä¸æ¹ã並åå¦çããã¨è¤æ°ã®ããã»ã¹ã§åæã«å¦çãããä¸åãããéããªã£ã¦ããã
ç°å¢ï¼Python 3.4.0, Windows8, Core i7 4650U
cpu : 1 MainProcess : This started at 13:01:27. MainProcess : This started at 13:01:28. MainProcess : This started at 13:01:29. MainProcess : This started at 13:01:30. MainProcess : This started at 13:01:31. MainProcess : This started at 13:01:32. MainProcess : This started at 13:01:33. MainProcess : This started at 13:01:34. MainProcess : This started at 13:01:35. MainProcess : This started at 13:01:36. answer : 285 time : 10.006 s cpu : 4 SpawnPoolWorker-3 : This started at 13:01:37. SpawnPoolWorker-2 : This started at 13:01:37. SpawnPoolWorker-4 : This started at 13:01:37. SpawnPoolWorker-1 : This started at 13:01:37. SpawnPoolWorker-3 : This started at 13:01:38. SpawnPoolWorker-2 : This started at 13:01:38. SpawnPoolWorker-1 : This started at 13:01:38. SpawnPoolWorker-4 : This started at 13:01:38. SpawnPoolWorker-3 : This started at 13:01:39. SpawnPoolWorker-2 : This started at 13:01:39. answer : 285 time : 3.221 s
ä¾ï¼
ãæ±ãããï¼n=1000ã®ã¨ãï¼
è¨ç®ä¸ã®å·¥å¤«ã¯ããããã§ããã ãããã©ãããã§ã¯ãã¤ã¼ããªå®è£ ã§ä¸¦åå¦çã®å¹æãè¦ãã
ã½ã¼ã¹ã³ã¼ã
from multiprocessing import Pool, cpu_count import time def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) def f(an): a, n = an t = 0 for b in range(a, n+1): for c in range(b, n+1): t += gcd(gcd(a,b), c) return t if __name__ == '__main__': n = 1000 print('cpu : %d' % 1) st = time.time() ans = 0 for a in range(1, n+1): for b in range(a, n+1): for c in range(b, n+1): ans += gcd(gcd(a, b), c) print('answer : %d' % ans) print('time : %.3f s' % (time.time()-st)) print('\ncpu : %d' % cpu_count()) st = time.time() p = Pool() ans = sum(p.map(f, [(a,n) for a in range(1, n+1)])) print('answer : %d' % ans) print('time : %.3f s' % (time.time()-st))
çµæ
Pythonã¨PyPyã®ä¸¡æ¹ã§1.5åãããã®é度ã«ãªã£ãã
ç°å¢ï¼Python 3.4.0, Windows8, Core i7 4650U
cpu : 1 answer : 229638478 time : 363.790 s cpu : 4 answer : 229638478 time : 237.350 s
ç°å¢ï¼PyPy 2.3.1, Windows8, Core i7 4650U
cpu : 1 answer : 229638478 time : 17.393 s cpu : 4 answer : 229638478 time : 11.338 s
pypyéãï¼
ã¾ã¨ã
確ãã«CPU使ç¨ç100%ã«ãªã£ããã©ç¹ã«ä¾ï¼ã§ã¯æã£ãããã¯é«éåãããªãã£ãã
è¤æ°ã®ããã»ã¹ã®çæã»ç®¡çããã¼ã¿ã®ããã¨ãã«ãªã¼ãã¼ããããããã®ã§è¨è¨ã«ãã£ã¦ã¯éã«é
ããªããã¨ãããã
ç°¡åã«ä½¿ããã®ã§ã´ãªæ¼ãã³ã¼ããå°ãã§ãéããããã¨ãã«è©¦ãã¦ã¿ã¦ãè¯ãããã
ãããªã£ã¦ããã¨6ã³ã¢12ã¹ã¬ããã®ãã·ã³ã¨ãã§å®è¡ããããªãâ¦