Pythonããã°ã©ã ã§1000äž
çªç®ã®ãã£ããããæ°ãèšç®ãããšããŸãã ã³ã³ãã¥ãŒã¿ãŒã§ç°¡åãªã¢ã«ãŽãªãºã ã䜿çšããé¢æ°ã¯ã25å以äžèšç®ãå®è¡ããŸãã ãã ããé¢æ°ã«ç¹å¥ãªæé©åãã³ã¬ãŒã¿ãé©çšãããšãé¢æ°ã¯ããã18ç§ã§çããèšç®ããŸãïŒ
85åé«é ïŒïŒ
å®éãããã°ã©ã ãå®è¡ããåã«ãPythonã€ã³ã¿ãŒããªã¿ãŒã¯ãã¹ãŠã®éšåãç¹å¥ãªãã€ãã³ãŒãã«ã³ã³ãã€ã«ããŸãã SkidanovAlexãããŠãŒã¶ãŒã«ãã£ãŠèšè¿°ãããæ¹æ³ã䜿çšããŠããã®ãã³ã¬ãŒã¿ãŒã¯çµæã®é¢æ°ãã€ãã³ãŒããåæããããã§äœ¿çšãããã¢ã«ãŽãªãºã ãæé©åããããšããŸãã ããã«ããã®æé©åã¯ããã°ã©ã ãç¹å®ã®åæ°ã§ã¯ãªã挞è¿çã«å éã§ããããšãããããŸãã ãããã£ãŠãã«ãŒãå
ã®å埩åæ°ãå€ãã»ã©ãæé©åãããé¢æ°ã¯å
ã®é¢æ°ã«æ¯ã¹ãŠå éãããåæ°ãå¢ããŸãã
ãã®èšäºã§ã¯ããã³ã¬ãŒã¿ãŒããã®ãããªæé©åããã€ã©ã®ããã«ç®¡çãããã«ã€ããŠèª¬æããŸãã ãã®ãã³ã¬ãŒã¿ãå«ã
cpmoptimizeã©ã€ãã©ãªãèªåã§ããŠã³ããŒãããŠãã¹ãããããšãã§ããŸãã
çè«
2幎åã
SkidanovAlexã¯ã次ã®æäœããµããŒãããå¶éãããèšèªã€ã³ã¿ãŒããªã¿ãŒã説æããèå³æ·±ãèšäºãå
¬éããŸããã
èšèªã®å€æ°ã®å€ã¯æ°å€ã§ããå¿
èŠãããããã®ãµã€ãºã¯å¶éãããŠããŸããïŒ
é·ãæŒç®ããµããŒããããŠããŸãïŒã å®éãå€æ°ã¯Pythonã®æŽæ°ãšããŠä¿åãããæ°å€ãããŒããŠã§ã¢ã§ãµããŒããããŠãã4ãã€ããŸãã¯8ãã€ãã¿ã€ãã®å¢çãè¶
ãããšãé·ãç®è¡ã¢ãŒãã«åãæ¿ãããŸãã
é·ãæŒç®ã䜿çšãããªãå ŽåãèããŠãã ããã ãã®å Žåãæäœã®å®è¡æéã¯å€æ°ã®å€ã«äŸåããŸãããã€ãŸããã«ãŒãã§ã¯ãã¹ãŠã®å埩ãåæã«å®è¡ãããŸãã ãã®ãããªã³ãŒããåŸæ¥ã®ã€ã³ã¿ãŒããªã¿ãŒã®ããã«ãé¡ãã§å®è¡ãããšããµã€ã¯ã«ã¯æéå
ã«å®äºããŸã
ããã§ã
nã¯å埩åæ°ã§ãã ã€ãŸãã
nåã®å埩ã®ã³ãŒããæé
tã§æ©èœããå Žåã次ã®ã³ãŒãã¯
ç¹°ãè¿ãã¯æéãšãšãã«æ©èœããŸã
ïŒ
èšç®ã®è€éããåç
§ïŒã
å
ã®èšäºã®ã€ã³ã¿ãŒããªã¿ãŒã¯ãç¹å®ã®ãããªãã¯ã¹åœ¢åŒã®å€æ°ã䜿çšããŠåèšèªã§ãµããŒããããŠããæäœã瀺ããå€æ°ã®å
ã®å€ãæã€ãã¯ãã«ã«ä¹ç®ããŸãã ä¹ç®ã®çµæãšããŠãå€æ°ã®æ°ããå€ãæã€ãã¯ãã«ãååŸããŸãã
ãããã£ãŠãäžé£ã®æäœãå®è¡ããã«ã¯ããã¯ãã«ã«æ¬¡ã®æäœã«å¯Ÿå¿ããè¡åãè¡åã§ä¹ç®ããå¿
èŠããããŸãã å¥ã®æ¹æ³-çµåæ§ã䜿çšããŠãè¡åãæåã«äºãã«ä¹ç®ãã次ã«çµæã®ç©ã«ãã£ãŠå
ã®ãã¯ãã«ãä¹ç®ããããšãã§ããŸãã
ã«ãŒããå®äºããã«ã¯ã
n-ã«ãŒãå
ã®å埩åæ°ãããã³ãããªãã¯ã¹
B-ã«ãŒãã®æ¬äœããã®æäœã«å¯Ÿå¿ãããããªãã¯ã¹ã®é£ç¶ç©ãèšç®ããå¿
èŠããããŸãã 次ã«ãå
ã®ãã¯ãã«ã«è¡å
Bã nåæããå¿
èŠããããŸãã å¥ã®æ¹æ³-æåã«è¡å
Bã
nä¹ãã次ã«ãã¯ãã«ã«çµæãä¹ããããšãã§ããŸãïŒ
ãã€ããªã¹ãä¹ã¢ã«ãŽãªãºã ã䜿çšãããšããµã€ã¯ã«ãã¯ããã«çãæéã§å®äºããããšãã§ããŸã
ã ãããã£ãŠã
nåã®å埩ã®ã³ãŒããæé
tã§æ©èœããå Žåã
ç¹°ãè¿ãã¯æéãšãšãã«æ©èœããŸã
ïŒåã®ã±ãŒã¹ã®ããã«ã2åã ãé
ãã
nåé
ãã¯ãããŸããïŒã
ãã®çµæãäžã§æžãããããã«ããµã€ã¯ã«
nã®å埩åæ°ã倧ãããªãã»ã©ãæé©åãããé¢æ°ãå éããåæ°ãå¢ããŸãã ãããã£ãŠãæé©åã®å¹æã¯ã
nã倧ãã
å Žåã«ç¹ã«é¡èã«ãªããŸãã
é·ãç®è¡æŒç®ããŸã 䜿çšãããŠããå Žåãäžèšã®æéæšå®ã¯æ£ãããªãå ŽåããããŸãã ããšãã°ã
nçªç®ã®ãã£ããããæ°ãèšç®ãããšããå埩ããå埩ãŸã§ã®å€æ°ã®å€ã¯ãŸããŸã倧ãããªãããããã䜿çšããæäœã¯é
ããªããŸãã ãã®å Žåãããã°ã©ã ã®å®è¡æéã®æŒžè¿çãªæšå®ãè¡ãããšã¯ããå°é£ã§ãïŒåå埩ã§å€æ°ã®æ°å€ã®é·ããšãæ°å€ã®ä¹ç®ã«äœ¿çšãããæ¹æ³ãèæ
®ããå¿
èŠããããŸãïŒãããã§ã¯èª¬æããŸããã ããã§ãããã®æé©åææ³ãé©çšãããšããã®å Žåã§ã挞è¿çãªåäœãæ¹åãããŸãã ããã¯ããã£ãŒãã«ã¯ã£ãããšè¡šç€ºãããŸãã
ã¢ã€ãã¢
æ°å幎åã«ããã°ã©ããŒãããã°ã©ã å
ã®å€æ°ã«4ãæããå¿
èŠãããå Žåã圌ã¯ä¹ç®æŒç®ã䜿çšããŸããããåçã®é«éåŠç-å·Šã«2ãããã·ããããŸããçŸåšãã³ã³ãã€ã©ãŒèªäœããã®ãããªæé©åãè¡ãããšãã§ããŸãã éçºæéãççž®ããããã®åªåã®äžã§ãé«ã¬ãã«ã®æœè±¡åãåããèšèªãæ°ãã䟿å©ãªãã¯ãããžãŒãããã³ã©ã€ãã©ãªãŒãç»å ŽããŸããã ããã°ã©ã ãæžããšããéçºè
ã¯ã»ãšãã©ã®æéãã³ã³ãã¥ãŒã¿ãŒã«ããã°ã©ã ãäœããã¹ããã説æãïŒæ°åã«4ãæããïŒãå¹æçãª
æ¹æ³ã§ã¯ãããŸããïŒãããã·ããã䜿çšïŒã ãããã£ãŠãå¹ççãªã³ãŒããäœæããã¿ã¹ã¯ã¯ãã³ã³ãã€ã©ãŒãšã€ã³ã¿ãŒããªã¿ãŒã«éšåçã«è»¢éãããŸãã
ã³ã³ãã€ã©ã¯ãããŸããŸãªæäœãããå¹ççãªæäœã«çœ®ãæããããåŒã®å€ãäºæž¬ããããã³ãŒãã®äžéšãåé€ãŸãã¯äº€æãããã§ããããã«ãªããŸããã ããããã³ã³ãã€ã©ã¯ãŸã ã人ãæžããèšç®
ã¢ã«ãŽãªãºã ã挞è¿çã«ããå¹ççãª
ã¢ã«ãŽãªãºã ã«çœ®ãæããŠããŸããã
å
ã®èšäºã§èª¬æããæ¹æ³ã®å®è£
ã¯ãå®éã®ããã°ã©ã ã§äœ¿çšããã«ã¯åé¡ããããŸãããã®ããã«ã¯ãã³ãŒãã®äžéšãç¹å¥ãªèšèªã§æžãæããå¿
èŠãããããã®ã³ãŒããšããåãããã«ã¯ãã€ã³ã¿ãŒããªã¿ãŒã§ãµãŒãããŒãã£ã®ã¹ã¯ãªãããå®è¡ããå¿
èŠããããŸãã ãã ãããã®èšäºã®èè
ã¯ãã³ã³ãã€ã©ãŒã®æé©åã«ãããŠå®éã«åœŒã®ãã¹ããã©ã¯ãã£ã¹ã䜿çšããããšãææ¡ããŠããŸãã Pythonããã°ã©ã ãæé©åããããã«ãå®éã®ãããžã§ã¯ãã§ã®äœ¿çšã«æ¬åœã«é©ãã圢åŒã§ãã®ã¡ãœãããå®è£
ããããšããŸããã
ç§ãæžããã©ã€ãã©ãªã䜿çšããå Žåãæé©åãé©çšããã«ã¯ãé«éåããå¿
èŠãããé¢æ°ã®åã«ãç¹å¥ãªãã³ã¬ãŒã¿ã䜿çšããŠ1è¡è¿œå ããã ãã§ååã§ãã ãã®ãã³ã¬ãŒã¿ã¯ãå¯èœã§ããã°ç¬ç«ããŠãµã€ã¯ã«ãæé©åãããããªãç¶æ³ã§ãããã°ã©ã ããäžæãããŸããã
䜿çšäŸ
ãã³ã¬ãŒã¿ã圹ç«ã€äŸ¿å©ãªã¿ã¹ã¯ãããã€ãèããŠã¿ãŸãããã
äŸ1 é·ããµã€ã¯ã«
å³ã«ç€ºãããŠããã«ãŒã«ã«å¯Ÿå¿ããã·ãŒã±ã³ã¹ãããããã®
nçªç®ã®é
ãèšç®ããå¿
èŠããããšããŸãã
ãã®å Žåãã·ãŒã±ã³ã¹ã®æ¬¡ã®ã¡ã³ããŒãã©ã®ããã«è¡šç€ºããããã¯çŽæçã«ããããŸãããããã«å¯Ÿå¿ããæ°åŒãèŠã€ããŠèšŒæããã«ã¯æéãããããŸãã ããã§ããã«ãŒã«ãèšè¿°ããç°¡åãªã¢ã«ãŽãªãºã ãèšè¿°ããã³ã³ãã¥ãŒã¿ãŒèªäœãåé¡ã®çãããã°ããèªãæ¹æ³ãèŠã€ãåºãããšãææ¡ã§ããŸãã
from cpmoptimize import cpmoptimize, xrange @cpmoptimize() def f(n): step1 = 1 step2 = 1 res = 1 for i in xrange(n): res += step2 step2 += step1 step1 += 1 return res
èå³æ·±ãããšã«ããã®é¢æ°ã
ããšã®ã«ãŒã
ç¹°ãè¿ãã æé©åããªããã°ããã®ãããªé¢æ°ã¯èããããæéåäœãçµäºããŸããã§ãããããã³ã¬ãŒã¿ãŒã䜿çšãããšãæ£ããçµæãè¿ããªãã1ç§ãããããŸããã
äŸ2 ãã£ããããæ°
nçªç®ã®ãã£ããããæ°ãè¿
éã«èšç®ããããã«ãè¡åã环ä¹ãããšããåãèãã«åºã¥ããé«éã§ãããªããéèŠãª
ã¢ã«ãŽãªãºã ããããŸã ã çµéšè±å¯ãªéçºè
ã§ããã°ãæ°åã§å®è£
ã§ããŸãã ãã®ãããªèšç®ãå®è¡ãããã³ãŒããããã«æ©èœããå¿
èŠããªãå ŽåïŒããšãã°ã1äžæªæºã®æ°å€ã§ãã£ããããæ°ãèšç®ããå¿
èŠãããå ŽåïŒãäœæ¥æéãç¯çŽããããã«ãéçºè
ã¯ã»ãšãã©ã®å ŽåãåŽåãç¯çŽããŠãœãªã¥ãŒã·ã§ã³ãèšè¿°ããããšã決å®ããŸãé¡ãã
ããã§ãããã°ã©ã ããã°ããåäœãããå¿
èŠãããå Žåã¯ãåªåããŠè€éãªã¢ã«ãŽãªãºã ãèšè¿°ãããããã³ã¬ãŒã¿ãé¢æ°ã«é©çšããŠèªåæé©åæ¹æ³ã䜿çšããå¿
èŠããããŸãã
nãååã«å€§ããå Žåãã©ã¡ãã®å Žåãããã°ã©ã ã®ããã©ãŒãã³ã¹ã¯ã»ãŒåãã«ãªããŸããã2çªç®ã®å Žåãéçºè
ã¯èªåã®äœæ¥æéãããå°ãªãæéãè²»ãããŸãã
å°ãã
nãšå€§ãã
nã®è¡šãšã°ã©ãã§ã以äžã§èª¬æããã¡ãœããã®å®è¡æéãæ¯èŒã§ããŸãã
å
¬å¹³ãæãããã«ããã£ããããæ°ãèšç®
ããããã®
é«é2ååãšåŒã°ããå¥ã®ã¢ã«ãŽãªãºã ãããããšã«æ³šæããŠãã ããã äžå¿
èŠãªæ°ã®å ç®ãšä¹ç®ãé€å€ãããŠããããã圌ã¯ä»¥åã®ã¢ã«ãŽãªãºã ã«ããåã¡ãŸããã ãã®ã¢ã«ãŽãªãºã ã®èšç®æéããæ¯èŒã®ããã«ã°ã©ãã«è¡šç€ºãããŸãã
æéã®çµæ Function "fib": (*) Testcase "big": +--------+----------+--------------+--------------+--------------+-------+ | arg | naive, s | cpm, s | matrices, s | fast dbl, s | match | +--------+----------+--------------+--------------+--------------+-------+ | 0 | 0.00 | 0.00 | 0.00 | 0.00 | True | | 20000 | 0.01 | 0.00 (2.5x) | 0.00 (3.0x) | 0.00 (5.1x) | True | | 40000 | 0.03 | 0.01 (5.7x) | 0.00 (1.8x) | 0.00 (4.8x) | True | | 60000 | 0.06 | 0.01 (7.9x) | 0.01 (1.4x) | 0.00 (4.8x) | True | | 80000 | 0.11 | 0.01 (9.4x) | 0.01 (1.3x) | 0.00 (4.6x) | True | | 100000 | 0.16 | 0.01 (11.6x) | 0.01 (1.1x) | 0.00 (4.5x) | True | | 120000 | 0.23 | 0.02 (11.8x) | 0.02 (1.2x) | 0.00 (4.7x) | True | | 140000 | 0.32 | 0.02 (14.7x) | 0.02 (1.1x) | 0.00 (4.1x) | True | | 160000 | 0.40 | 0.03 (13.1x) | 0.03 (1.2x) | 0.01 (4.6x) | True | | 180000 | 0.51 | 0.03 (14.7x) | 0.03 (1.1x) | 0.01 (4.4x) | True | | 200000 | 0.62 | 0.04 (16.6x) | 0.03 (1.1x) | 0.01 (4.4x) | True | | 220000 | 0.75 | 0.05 (16.1x) | 0.04 (1.1x) | 0.01 (4.9x) | True | | 240000 | 0.89 | 0.05 (17.1x) | 0.05 (1.0x) | 0.01 (4.7x) | True | | 260000 | 1.04 | 0.06 (18.1x) | 0.06 (1.0x) | 0.01 (4.5x) | True | | 280000 | 1.20 | 0.06 (20.2x) | 0.06 (1.0x) | 0.01 (4.2x) | True | | 300000 | 1.38 | 0.07 (19.4x) | 0.07 (1.1x) | 0.02 (4.4x) | True | +--------+----------+--------------+--------------+--------------+-------+
å®éã«ã¯ãããç¥ãããŠããç¹°ãè¿ãåŒã®ä»£ããã«æ°åã®ã·ãŒã±ã³ã¹ã䜿çšãããšãããè€éãªç¶æ³ã«çŽé¢ããå¯èœæ§ããããŸãã
ããã¯ãããšãã°æ¬¡ã®ãããªããè€éãªåŒã§äžããããŸãã
ãã®å Žåãã€ã³ã¿ãŒãããäžã§äžèšã®ã¢ã«ãŽãªãºã ã®å®è£
ãèŠã€ããããšã¯ã§ããŸããããè¡åã环ä¹ããã¢ã«ãŽãªãºã ãæåã§èŠã€ããã«ã¯ããªãã®æéãããããŸãã ããã«ããããããããœãªã¥ãŒã·ã§ã³ããé¡ããšããŠèšè¿°ãããã³ã¬ãŒã¿ãé©çšããããšãã§ããŸããããããã°ãã³ã³ãã¥ãŒã¿ãŒã¯ç°¡åãªã¢ã«ãŽãªãºã ãæãä»ããŸãã
from cpmoptimize import cpmoptimize @cpmoptimize() def f(n): prev3 = 0 prev2 = 1 prev1 = 1 for i in xrange(3, n + 3): cur = 7 * (2 * prev1 + 3 * prev2) + 4 * prev3 + 5 * i + 6 prev3, prev2, prev1 = prev2, prev1, cur return prev3
äŸ3 çæ¯æ°åã®åèš
çæ¯æ°åã®
countã¡ã³ããŒã®åèšãèšç®ããå¿
èŠããããšããŸããæåã®ã¡ã³ããŒ
startãšåæ¯
coeffãäžããããŸãã ãã®ã¿ã¹ã¯ã§ã¯ããã³ã¬ãŒã¿ã¯ç°¡åãªãœãªã¥ãŒã·ã§ã³ãæé©åã§ããŸãã
from cpmoptimize import cpmoptimize @cpmoptimize() def geom_sum(start, coeff, count): res = 0 elem = start for i in xrange(count): res += elem elem *= coeff return res
äŸçªå·4ã ã¹ãä¹
æ°
aãéè² ã®æŽæ°
nã«çŽ¯ä¹ããå¿
èŠããããšããŸãã ãã®ãããªã¿ã¹ã¯ã§ã¯ããã³ã¬ãŒã¿ã¯å®éã«ç°¡åãªè§£æ±ºçã
ãã€ããªã¹ãä¹ã¢ã«ãŽãªãºã ã«çœ®ãæããŸãã
from cpmoptimize import cpmoptimize @cpmoptimize() def power(a, n): res = 1 for i in xrange(n): res *= a return res
ããŠã³ããŒããªã³ã¯
pipã䜿çšããŠã©ã€ãã©ãªãã€ã³ã¹ããŒã«ã§ããŸãã
sudo pip install cpmoptimize
ãµã³ãã«ä»ãã®ã©ã€ãã©ãªã®ãœãŒã¹ã³ãŒãã¯ãç¡æã®MITã©ã€ã»ã³ã¹ã®äžã§
githubã§å
¥æã§ããŸãã
UPDã ããã±ãŒãžã¯Python Package Indexã§
å
¬éãããŠããŸãã
æé©åã®äŸã¯ãã
tests / ããã©ã«ããŒã«ãããŸãã åäŸã¯ããçŽæ¥çãªããœãªã¥ãŒã·ã§ã³ãæé©åãããããŒãžã§ã³ãè¡šãé¢æ°ãããã³ç¹å®ã®åé¡ã«å¯Ÿããããç¥ãããè€éã ãè¿
éãªãœãªã¥ãŒã·ã§ã³ãè¡šãé¢æ°ã§æ§æãããŠããŸãã ãããã®é¢æ°ã¯ãããŸããŸãªãã¹ãã°ã«ãŒãã§å®è¡ããã¹ã¯ãªããã«é
眮ãããäœæ¥æéã枬å®ãã察å¿ããããŒãã«ãæ§ç¯ããŸãã
matplotlibã©ã€ãã©ãªã
ã€ã³ã¹ããŒã«ãããŠããå Žåãã¹ã¯ãªããã¯å
¥åããŒã¿ïŒéåžžãŸãã¯å¯Ÿæ°ïŒã«å¯Ÿããé¢æ°ã®ã©ã³ã¿ã€ã ã®äŸåé¢ä¿ã®ã°ã©ããäœæãããããã "
tests / plots / "ãã©ã«ããŒã«ä¿åããŸãã
ã©ã€ãã©ãªã®èª¬æ
ã©ã€ãã©ãªãäœæããéãPythonã€ã³ã¿ãŒããªã¿ãŒãäœæããããã°ã©ã ã®ãã€ãã³ãŒãã解æããã€ã³ã¿ãŒããªã¿ãŒã«å¹²æžããããšãªãå®è¡æã«å€æŽã§ãããšããäºå®ãå©çšããŸãããããã«ãããèšèªãæ¡åŒµããå¯èœæ§ãåºãããŸãïŒããšãã°ã
gotoã³ã³ã¹ãã©ã¯ãã®å®è£
ãåç
§ïŒã éåžžãPythonã«çµã¿èŸŒãŸããŠããé·ãæŒç®ã䜿çšãããšã説æããæ¹æ³ã®å©ç¹ãçŸããŸãã
ãããã®çç±ããã説æããæé©åãPythonã§å®è£
ããããšã¯ãç§ã«ãšã£ãŠããèå³æ·±ãã¿ã¹ã¯ã«ãªããŸãããããã¡ãããC ++ã³ã³ãã€ã©ã§äœ¿çšãããšãããã«é«éãªããã°ã©ã ãäœæã§ããŸãã ããã«ããã³ã¬ãŒã¿ã«æé©åãããPythonã³ãŒãã¯ã挞è¿çãªåäœãåªããŠãããããC ++ã³ãŒãã®é¡ãè¿œãè¶ãåŸåããããŸãã
ãã€ãã³ãŒããå€æŽãããšãã
byteplayã¢ãžã¥ãŒã«ã䜿çšããŸããã
ãã®ã¢ãžã¥ãŒã«ã¯ããã€ãã³ãŒããå解ããã³çµã¿ç«ãŠãããã®äŸ¿å©ãªã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããŸãã æ®å¿µãªããããã®ã¢ãžã¥ãŒã«ã¯ãŸã Python 3ã«ç§»æ€ãããŠããªãããããããžã§ã¯ãå
šäœãPython 2.7ã§å®è£
ããŸããã
cpmoptimizeã©ã€ãã©ãªã®ååã¯ã
ããããªãã¯ã¹ã®é»åãèšç®ããŠæé©åãã ãã®ç¥ã§ãã ãã®èšäºã®ããªã¥ãŒã ã§ã¯ããã€ãã³ãŒãã®åæãšå€æŽã®ããã»ã¹ã詳现ã«èª¬æããããšã¯ã§ããŸãããããã®ã©ã€ãã©ãªãæäŸããæ©èœãšãã®äœæ¥ã®åºç€ãšãªãååã«ã€ããŠè©³çŽ°ã«èª¬æããããšããŸãã
èªåã®xrange
cpmoptimizeã
xrange ïŒ
åæ¢ ïŒ
cpmoptimizeã
xrange ïŒ
start ã
stop [ã
step ]ïŒ
æé©åã®ããã®Python 2ã®
çµã¿èŸŒã¿xrangeåã¯ãåŒæ°ãšããŠ
longåã®
longæŽæ°ããµããŒãããŠããŸããã
>>> xrange(10 ** 1000) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: Python int too large to convert to C long
ã©ã€ãã©ãªã䜿çšããå Žåãé åºã®æäœã®æ°ã§åŸªç°ãããã
1ç§æªæºã§å®è¡ã§ããããã°ã©ã ã§äžè¬çã«ãªããŸããã©ã€ãã©ãªã¯ãçŽç²ãªPythonã§
xrangeåã®ç¬èªã®å®è£
ãæäŸããŸãïŒã©ã€ãã©ãªå
ã§ã¯
CPMRangeãšåŒã°ã
ãŸã ïŒã éåžžã®
xrangeã®ãã¹ãŠã®æ©èœãšãããã«
longåã®åŒæ°ããµããŒãããŸãã ãã®ãããªã³ãŒãã¯ãšã©ãŒã«ãªãããæ£ããçµæããã°ããèšç®ããŸãã
from cpmoptimize import cpmoptimize, xrange @cpmoptimize() def f(): res = 0 for i in xrange(10 ** 1000): res += 3 return res print f()
ããã§ããããªãã®å Žåããµã€ã¯ã«ã®å埩åæ°ãå°ãªãå Žåããã³ã¬ãŒã¿ã§
çµã¿èŸŒã¿ã®xrangeã䜿ãç¶ããããšãã§ããŸãã
Cpmoptimizeé¢æ°
cpmoptimizeã
cpmoptimize ïŒ
strict = Falseã
iters_limit = 5000ã
types =ïŒintãlongïŒã
opt_min_rows = Trueã
opt_clear_stack = TrueïŒ
ãã³ã¬ãŒã¿ã¢ããªã±ãŒã·ã§ã³
cpmoptimizeé¢æ°ã¯ãå®è¡ãããæé©åã®ãã©ã¡ãŒã¿ãŒãåãå
¥ãããããã®ãã©ã¡ãŒã¿ãŒãèæ
®ãããã³ã¬ãŒã¿ãŒãè¿ããŸãããã³ã¬ãŒã¿ãŒã¯ãæé©åãããé¢æ°ã«é©çšããå¿
èŠããããŸãã ããã¯ãç¹å¥ãªæ§æïŒãç¬ãèšå·ã®æç¡ã«ãããããïŒã䜿çšããŠå®è¡ã§ããŸãã
from cpmoptimize import cpmoptimize @cpmoptimize() def f(n):
ãã€ãã³ãŒããå€æŽããåã«ãå
ã®é¢æ°ãã³ããŒãããŸãã ãããã£ãŠãäžèšã®ã³ãŒãã§ã¯ãé¢æ°
fãš
smart_gã¯æçµçã«æé©åãããŸããã
naive_gã¯æé©åãããŸããã
ã«ãŒããæ€çŽ¢ãã
ãã³ã¬ãŒã¿ã¯ãé¢æ°ã®ãã€ãã³ãŒãã§æšæº
ã®ã«ãŒããæ¢ããŸããã
whileã«ãŒããšãžã§ãã¬ãŒã¿ãŒããã³ãªã¹ãåŒå
ã®
forã«ãŒãã¯ç¡èŠãããŸãã ãã¹ããããã«ãŒãã¯ãŸã ãµããŒããããŠããŸããïŒæãå
åŽã®ã«ãŒãã®ã¿ãæé©åãããŠããŸãïŒã
ãã®ä»ã®æ§é ã¯æ£ããåŠçãããŸãã
èš±å¯ãããã¿ã€ã
ãã³ã¬ãŒã¿ã¯ãã«ãŒãã§äœ¿çšãããå€æ°ã®ã¿ã€ãã«é¢ä¿ãªããã«ãŒããæé©åã§ããŸããã å®éãããšãã°Pythonã§ã¯ãä¹ç®ããšã«æååããã¡ã€ã«ã«æžã蟌ãã¿ã€ãã決å®ã§ããŸãã æé©åãé©çšããçµæãé¢æ°ã§è¡ãããä¹ç®ã®æ°ãå€ããå¯èœæ§ããããŸãã ãã®ããããã¡ã€ã«ã®è¡æ°ãç°ãªããæé©åã«ããããã°ã©ã ãäžæãããŸãã
ããã«ãè¡åã䜿çšããæäœã§ã¯ãå€æ°ãæé»çã«è¿œå ããã³ä¹ç®ããããããå€æ°ã®ã¿ã€ãããæ··åšããããããšãã§ããŸãã å®æ°ãŸãã¯å€æ°ã®ããããã
floatåã®å Žåãã³ãŒãã®å®è¡åŸã«
intåã«ãªã£ãŠããã¯ãã®å€æ°ã
floatåã«ãªãå¯èœæ§ããããŸãïŒ
intãš
floatãè¿œå ãããš
floatãè¿ããã
ãã ïŒã ãã®ãããªåäœã¯ãããã°ã©ã ã§ãšã©ãŒãåŒãèµ·ããå¯èœæ§ããããåãå
¥ããããŸããã
äžèŠãªå¯äœçšãé¿ããããããã³ã¬ãŒã¿ã¯ãã«ãŒãã§äœ¿çšããããã¹ãŠã®å®æ°ãšå€æ°ã
æå¹ãªã¿ã€ãã§ãã
å Žåã«ã®ã¿ã«ãŒããæé©åããŸãã åæã®æå¹ãªåã¯
intããã³
long ãããã³ãããããç¶æ¿ãããåã§ãã å®æ°ãŸãã¯å€æ°ã®ããããã
longåã®å Žåãã³ãŒãã®å®è¡åŸã«
intåã«ãªã£ãŠããã¯ãã®å€æ°ã
longåã«ããããšãã§ããŸãã ãã ããPythonã®
intãš
longã¯ååã«äºææ§ãããããããšã©ãŒã«ã€ãªããããšã¯ãããŸããã
æå¹ãªã¿ã€ãã®ã»ãããå€æŽããå ŽåïŒããšãã°ã
floatã®ãµããŒããè¿œå ããå ŽåïŒã
typesãã©ã¡ãŒã¿ãŒã§å¿
èŠãªã¿ã€ãã®ã¿ãã«ãæž¡ãå¿
èŠããããŸãã ãã®ã¿ãã«ã®äžéšã§ãããããã®ã¿ãã«ã®äžéšã§ããåããç¶æ¿ãããåã¯æå¹ãšèŠãªãããŸãã å®éã
å€ã«æå¹ãªåãããã
ã©ããã®ç¢ºèªã¯ãåŒ
isinstanceïŒvalueãtypesïŒã䜿çšããŠå®è¡ãããŸãã
ãµã€ã¯ã«æé©åæ¡ä»¶
èŠã€ãã£ãåãµã€ã¯ã«ã¯ãå€ãã®æ¡ä»¶ãæºããå¿
èŠããããŸãã ãããã®ããã€ãã¯ããã³ã¬ãŒã¿ã䜿çšãããšãã«ãã§ã«ãã§ãã¯ãããŠããŸãã
- ã«ãŒãã®æ¬äœã¯ã代å
¥åœä»€ãåé
ããã³äºé
æŒç®ã®ã¿ã§æ§æãããããããçµã¿åãããŠè€éãªåŒã«ããããšãã§ããŸãã æ¡ä»¶ãä»ã®é¢æ°ã®åŒã³åºãã returnããã³yieldã¹ããŒãã¡ã³ããªã©ãå«ããããšã¯ã§ããŸããã
- ãªãã©ã³ãã«ã¯ã äºæž¬å¯èœæ§ã®æ¡ä»¶ãå¿
èŠãªå ŽåããããŸãããµã€ã¯ã«ã®åå埩ã§ããããã®å€ã¯åãã§ãªããã°ãªãããåã®å埩ã§ã®èšç®ã®çµæã«äŸåããŠã¯ãªããŸããã ãã®å ŽåïŒ
- åé
ãã€ãã¹ã®ãªãã©ã³ããšåæ§ã«ãå ç®ããã³æžç®ã®ãã¹ãŠã®ãªãã©ã³ãã¯ãäºæž¬äžèœã§ããå¯èœæ§ããããŸãã
- ä¹ç®ã®å°ãªããšã1ã€ã®ãªãã©ã³ãã¯äºæž¬å¯èœã§ãªããã°ãªããŸããïŒå
ã®ã€ã³ã¿ãŒããªã¿ãŒã§ã¯å®æ°ã«ããä¹ç®ã®ã¿ããµããŒããããŠãããšããäºå®ã«äŒŒãŠããŸãïŒã
- ã¹ãä¹ãé€ç®ãå°äœæŒç®ãããã³ãããåäœæŒç®ã®ãã¹ãŠã®ãªãã©ã³ãã¯äºæž¬å¯èœã§ãªããã°ãªããŸããã
- ã«ãŒãã§äœ¿çšããããã¹ãŠã®å®æ°ã¯ãæå¹ãªã¿ã€ãã§ããå¿
èŠããããŸãã
ãããã®æ¡ä»¶ãæºãããããšããµã€ã¯ã«ã®åã«ããã©ããããèšå®ãããæ¡ä»¶ã®å¥ã®éšåããã§ãã¯ãããŸããããµã€ã¯ã«ã®å
ã®ãã€ãã³ãŒãã¯åé€ãããŸããã Pythonã¯åçåä»ãã䜿çšããããã次ã®æ¡ä»¶ã¯ã«ãŒããéå§ããçŽåã«ã®ã¿ãã§ãã¯ã§ããŸãã
- ã«ãŒããééãããªããžã§ã¯ãã«ã¯ããã®cpmoptimize.xrangeã©ã€ãã©ãªã®æšæºxrangeåãŸãã¯ãã®é¡äŒŒäœãå¿
èŠã§ãã ãã ãããªã¹ããè¿ãäœéç¯å²é¢æ°ã¯ãµããŒããããŠããŸããã
- ã«ãŒãã§äœ¿çšããããã¹ãŠã®å€æ°ã®å€ã¯æå¹ãªã¿ã€ãã§ãã
ããã©ããããæé©åã蚱容å¯èœã§ãããšçµè«ä»ããå Žåãå¿
èŠãªãããªãã¯ã¹ãèšç®ããã䜿çšãããå€æ°ã®å€ãæ°ãããã®ã«å€æŽãããŸãã æé©åãå®è¡ã§ããªãå Žåãã«ãŒãã®å
ã®ãã€ãã³ãŒããèµ·åãããŸãã
ãµã€ã¯ã«ããšã®åŒãšã³ãŒãã®åé€
説æãããŠããæ¹æ³ã¯ã¹ãä¹ãšãããåäœã®ANDæŒç®ããµããŒãããŠããªããšããäºå®ã«ããããããã次ã®ã³ãŒããæé©åãããŸãã
@cpmoptimize() def f(n, k): res = 0 for i in xrange(n): m = 3 res += ((k ** m) & 676) + i return res
ãã€ãã³ãŒããåæãããšãããã³ã¬ãŒã¿ã¯ãåŒ
ïŒk ** mïŒããã³676ã®å€æ°
kããã³
mã®å€ã¯ããããã䜿çšããããµã€ã¯ã«ã®å埩ã«äŸåããããããã£ãŠåŒå
šäœã®å€ã¯å€åããªããšçµè«ä»ããŸãã åå埩ã§èšç®ããå¿
èŠã¯ãããŸãã;ãã®å€ãäžåºŠèšç®ããã°ååã§ãã ãããã£ãŠã察å¿ãããã€ãã³ãŒãåœä»€ãåãåºããŠããµã€ã¯ã«ã®éå§çŽåã«å®è¡ãããµã€ã¯ã«å
ã®æ¢è£œã®å€ã眮ãæããããšãã§ããŸãã ã³ãŒãã¯æ¬¡ã®ããã«å€æãããŸãã
@cpmoptimize() def f(n, k): res = 0 m = 3 _const1 = (k ** m) & 676 for i in xrange(n): res += _const1 + i return res
(
loop-invariant code motion ). , , (
_const1 k ). .
. , « » , .
ãã³ã¬ãŒã¿ã¯ãè€æ°ã®å²ãåœãŠãéšåçã«ãµããŒãããŸããèŠçŽ ãã»ãšãã©ãªãå ŽåãPythonã¯ã¿ãã«ã䜿çšããã«ãã³ã¬ãŒã¿ã§ãµããŒããããŠãããã€ãã³ãŒããäœæããŸãã a, b = b, a + b
ãããå€iters_limit
äžèšã®ã°ã©ãã§ã¯ãå埩åæ°ãå°ãªãå Žåãæé©åããããµã€ã¯ã«ã®åäœãéåžžãããé
ããªãããšããããŸãããã®å Žåãè¡åãšåãã§ãã¯ã®æ§ç¯ã«ãŸã æéããããããã§ããé¢æ°ãå¯èœãªéãé«éã§ãå°æ°ã®å埩ã§åäœããå¿
èŠãããå Žåãiters_limitãã©ã¡ãŒã¿ãŒã䜿çšããŠæ瀺çã«ãããå€ãèšå®ã§ããŸãã次ã«ãæé©åãéå§ããåã®ããã©ãããã¯ãã«ãŒããå®äºããªããã°ãªããªãå埩åæ°ããã§ãã¯ãããã®æ°ãæå®ããããããå€ãè¶
ããªãå Žåãæé©åããã£ã³ã»ã«ããŸããæåã«ããããå€ã¯5000åã®å埩ã«èšå®ãããŸãã 2åã®å埩ããäœãèšå®ããããšã¯ã§ããŸãããæã奜ãŸãããããå€ã¯ãé¢æ°ã®æé©åãããããŒãžã§ã³ãšåæããŒãžã§ã³ã®åäœæéã«å¯Ÿå¿ããç·ã°ã©ãäžã®äº€ç¹ã§ãããããå€ããã®ããã«æ£ç¢ºã«èšå®ãããŠããå Žåããã®å Žåãé¢æ°ã¯æéã®ã¢ã«ãŽãªãºã ãéžæã§ããŸãïŒæé©åãŸãã¯åæïŒãå³æ Œãªãã©ã°
å Žåã«ãã£ãŠã¯ãæé©åãå¿
é ã§ãããã®ãããæé©åããã«å埩ã®ã«ãŒãå
ã«ããå Žåãããã°ã©ã ã¯åã«ããªãŒãºããŸãããã®å Žåãstrictãã©ã¡ãŒã¿ãŒãæäŸãããŸããæåã¯ããã®å€ã¯Falseã§ãããTrueã«èšå®ãããŠããå Žåãæšæºforã«ãŒãã®1ã€ãæé©åãããŠããªããšãäŸå€ãã¹ããŒãããŸãããã³ã¬ãŒã¿ãé©çšãã段éã§æé©åãé©çšã§ããªããšããäºå®ãæ€åºãããå ŽåãäŸå€cpmoptimize.recompiler.RecompilationErrorãããã«çºçããŸããã«ãŒãã®äŸã§ã¯ã2ã€ã®äºæž¬äžèœãªå€æ°ãä¹ç®ãããŸãã >>> from cpmoptimize import cpmoptimize >>> >>> @cpmoptimize(strict=True) ... def f(n, k): ... res = 0 ... for i in xrange(n): ... res += i * res ... return res ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "cpmoptimize/__init__.py", line 100, in upgrade_func index += analyze_loop(settings, code, index) + 1 File "cpmoptimize/__init__.py", line 59, in analyze_loop raise err cpmoptimize.recompiler.RecompileError: Multiplication of two unpredictable values is unsupported
ã«ãŒãã®å®è¡åã«ãã®æé©åãé©çšã§ããªãããšãæ€åºãããå ŽåïŒã€ãŸããå埩åã®å€ãŸãã¯å€æ°ã®ã¿ã€ãããµããŒããããŠããªãããšãå€æããå ŽåïŒãTypeErroräŸå€ãã¹ããŒãããŸãã >>> from cpmoptimize import cpmoptimize >>> >>> @cpmoptimize(strict=True) ... def f(iterable): ... res = 0 ... for elem in iterable: ... res += elem ... return res ... >>> f(xrange(30)) 435 >>> f(range(30)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 4, in f File "cpmoptimize/hook.py", line 170, in exec_loop raise err TypeError: Iterator in optimized loops must have type "xrange" instead of <type 'list'>
è¿œå ã®æé©åãªãã·ã§ã³
opt_min_rowsãšopt_clear_stackã® 2ã€ã®ãã©ã°ã¯ããããªãã¯ã¹ãæ§ç¯ãããšãã«è¿œå ã®æé©åã¡ãœããã䜿çšãã圹å²ãæãããŸããæåã¯ãªã³ã«ãªã£ãŠããããªãã«ããæ©èœã¯ãã¢ç®çã§ã®ã¿ååšããŸãïŒãããã§ããããã®æ¹æ³ã®æå¹æ§ãæ¯èŒã§ããŸãïŒãæé©åãããã³ãŒããå®è¡ãããšãçæããããããªãã¯ã¹ã®äžéšã®ã»ã«ã§é·ãæéã®ä¹ç®ãæéã®å€§åãå ããŸãããã®æéã®ãããããã»ã¹ãšæ¯èŒãããšãä»ã®ãã¹ãŠã®ã»ã«ã®å¢æ®ã«è²»ããããæéã¯ãããããã§ããåŒãåã³ã³ãã€ã«ããããã»ã¹ã§ããã³ã¬ãŒã¿ã¯äžéã®ä»®æ³ã€ã³ã¿ããªã¿ã¹ã¿ãã¯ã®äœçœ®ãæ
åœããå€æ°ãè€éãªèšç®ã®åŸãä»ã®å®éã®å€æ°ã«ãã§ã«æ ŒçŽãããŠããé·ãæ°å€ãæ®ãå ŽåããããŸãããããã®å€ãããäžåºŠä¿åããå¿
èŠã¯ãããŸãããããããªãã¯ã¹ã®ã»ã«ã«æ®ã£ãŠããã°ã©ã ã倧å¹
ã«é
ãããŸãããããªãã¯ã¹ãä¹ç®ãããšããããã®é·ãæ°å€ãå床ä¹ç®ããå¿
èŠãããããã§ããopt_clear_stackãªãã·ã§ã³ã¯ããã®ãããªå€æ°ãã¯ãªã¢ãã責任ããããŸãã䜿çšã®æåŸã«ãããããŒãã«å²ãåœãŠããšããããã®é·ãå€ã¯æ¶ããŸãããã®ãªãã·ã§ã³ã¯ãããã°ã©ã ãéåžžã«å€§ããªæ°ã§åäœããå Žåã«ç¹ã«å¹æçã§ãããã®ãããªæ°å€ã®äžå¿
èŠãªä¹ç®ãæé€ããããšã«ãããããã°ã©ã ã2å以äžé«éåã§ããŸããOpt_min_rowsãªãã·ã§ã³ä¹ç®ããæ£æ¹è¡åã®ãµã€ãºãæå°åããå¿
èŠããããŸããæªäœ¿çšããã³äºæž¬å¯èœãªå€æ°ã«å¯Ÿå¿ããè¡ã¯ãè¡åããé€å€ãããŸããã«ãŒãå€æ°èªäœã䜿çšãããªãå Žåãæ£ããå€ãç¶æãã責任ãããè¡ãé€å€ãããŸããããã°ã©ã ãäžæãããªãããã«ããµã€ã¯ã«åŸããããã®å€æ°ã«ã¯ãã¹ãŠæ£ããæçµå€ãå²ãåœãŠãããŸããããã«ãå
ã®èšäºã§ã¯ãå€æ°ã®ãã¯ãã«ã«åäžæ§ã«çããèŠçŽ ãè¿œå ãããŸããããã®èŠçŽ ã«å¯Ÿå¿ããè¡ã䜿çšãããŠããªãå Žåããããé€å€ãããŸãããã®ãªãã·ã§ã³ã¯ãæ°å€ããŸã ããã»ã©é·ããªã£ãŠãããããããªãã¯ã¹ã®æ¬¡å
ãããã°ã©ã ã®å®è¡æéã«å€§ããè²¢ç®ããŠããå Žåãnãããã»ã©å€§ãããªãå Žåã«ããã°ã©ã ããããã«é«éåã§ããŸããäž¡æ¹ã®ãªãã·ã§ã³ãåæã«äœ¿çšãããšãä»®æ³å€æ°ã¯äºæž¬å¯èœãªïŒãŒãïŒå€ãæã¡å§ããopt_min_rowsã¯ããã«å¹ççã«æ©èœããŸããèšãæãããšãäž¡æ¹ã®ãªãã·ã§ã³ã®æå¹æ§ã¯ãããããã®ãªãã·ã§ã³ã®æå¹æ§ãããåªããŠããŸãã以äžã®ã°ã©ãã¯ãç¹å®ã®ãªãã·ã§ã³ãç¡å¹ã«ããå Žåã®ãã£ããããæ°ãèšç®ããããã®ããã°ã©ã ã®åäœæéã瀺ããŠããŸãïŒã-mãã¯opt_min_rowsãç¡å¹ãªããã°ã©ã ãã-cãã¯opt_clear_stackãç¡å¹ãªããã°ã©ã ãã-mcãã¯äž¡æ¹ã®ãªãã·ã§ã³ãããã«ç¡å¹ãªããã°ã©ã ã§ãïŒ ïŒïŒä»ã«äœãå®è£
ã§ããŸããïŒ
äžé£ã®æäœãå€æŽãã
以äžã®å Žåã ãã®ã¡ãœããã¯ããã€ãã®æäœããµããŒããããšèšããŸãã- 2ã€ã®å€æ°ããŸãã¯å€æ°ãšå®æ°ã«å¯ŸããŠæäœãå®è¡ã§ããŸãã
- å€æ°ãšå®æ°ã«å¯ŸããŠæäœãå®è¡ã§ããŸãã
ã¡ãœãããããã€ãã®æäœããµããŒãããŠããããšã¯æ¢ã«ç¥ã£ãŠããŸãã ã
確ãã«ïŒ- 2ã€ã®å€æ°ãŸãã¯å€æ°ãšå®æ°ãè¿œå ã§ããŸãã
- å€æ°ã«å®æ°ãæããããšãã§ããŸãã
çŸåšãæé©åãã³ã¬ãŒã¿ã«å®è£
ãããŠããã®ã¯ãããã®æäœã§ãããã ãã説æãããŠããæ¹æ³ã¯ãïŒä¹ç®ãšã¹ãä¹ã®ãã¢ïŒãªã©ã®ä»ã®æäœã®ãã¢ããµããŒãããŠããããšãããããŸããããšãã°ãç¹°ãè¿ãé¢ä¿ã§æå®ãããã·ãŒã±ã³ã¹ã®nçªç®ã®çªå·ãèŠã€ããããšãã¿ã¹ã¯ã®å Žåã§ãããã³ã¬ãŒã¿ãŒãããã€ãã®æäœããµããŒãããŠããå Žåãå€æ°ã«å¥ã®å€æ°ãæããããšãã§ããŸãïŒãã ããå€æ°ãè¿œå ããããšã¯ã§ããŸããïŒããã®å Žåããã®åé¡ã«å¯Ÿãã次ã®ç°¡åãªè§£æ±ºçã¯ããã³ã¬ãŒã¿ã«ãã£ãŠå éã§ããŸãã def f(n): a = 1 b = 1 for i in xrange(n): a, b = b, a * b return a
ã¡ãœãããæäœã®ãã¢ããµããŒãããŠããããšã蚌æã§ããŸã ã
ãããŠ
ïŒããã§ããããåäœã®AND ãŸãã¯ãããåäœã®ORã§ãïŒãæ£ã®æ°ã®å Žåãæäœã®ãã¢ã§äœæ¥ã§ããŸã ãããŠ
ã
ããšãã°ãæäœã®ãµããŒããå®è£
ããã«ã¯ãå€æ°ã®å€ã§ã¯ãªãããããã®å€ã®å¯Ÿæ°ã§æäœããããšãã§ããŸãã次ã«ãå€æ°ã®ä¹ç®ã¯å¯Ÿæ°ã®å ç®ã«çœ®ãæããããå€æ°ã®å®æ°ã®çŽ¯ä¹ã¯ã察æ°ã«å®æ°ãä¹ç®ããããšã«ããè¡ãããŸãããããã£ãŠãæã
ã¯åé¡ãæäœã§æ¢ã«å®çŸãããã±ãŒã¹ã«æžãããŸã ã
æå®ãããæ®ãã®æäœãã¢ã®ãµããŒããå®è£
ããæ¹æ³ã®èª¬æã«ã¯ãäžå®ã®æ°ã®æ°åŠèšç®ãå«ãŸããŠãããå¥ã®èšäºã«å€ããŸããçŸæç¹ã§ã¯ãæäœã®ãã¢ã¯ããçšåºŠäºææ§ãããããšã«èšåããã ãã§ããå
¥ãåã«ãŒã
ãã€ãã³ãŒãã§ã«ãŒãåæã¢ã«ãŽãªãºã ãæ¹è¯ããããšã«ããããã¹ããããã«ãŒãã®ãµããŒããå®è£
ããŠã次ã®ãããªã³ãŒããæé©åã§ããŸãã def f(): res = 0 for i in xrange(10 ** 50): for j in xrange(10 ** 100): res += i * 2 + j return res
äºæž¬å¯èœãªæ¡ä»¶
次ã®ã³ãŒãã§ã¯ãã«ãŒãæ¬äœã®æ¡ä»¶ã¯äºæž¬å¯èœã§ãããµã€ã¯ã«ãéå§ããåã«ããããæ£ãããã©ããã確èªããå®è¡ãããŠããªãã³ãŒããã©ã³ããåé€ã§ããŸããçµæã®ã³ãŒãã¯æé©åã§ããŸãïŒ def f(mode): res = 0 for i in xrange(10 ** 50): if mode == 1: res += 3 else: res += 5 return res
çµè«
äžè¬çã«ãPythonã€ã³ã¿ãŒããªã¿ãŒã¯ãäœæãããœãŒã¹ã³ãŒããšã»ãŒæ£ç¢ºã«äžèŽããäºæž¬å¯èœãªãã€ãã³ãŒããçæããŸããããã©ã«ãã§ã¯ãé¢æ°ã®ã€ã³ã©ã€ã³å±éãã«ãŒãã®å±éãããã°ã©ã ã®åäœã®åæãå¿
èŠãšãããã®ä»ã®æé©åã¯å®è¡ãããŸãããããŒãžã§ã³2.6以éã®ã¿ãCPythonã¯å®æ°æŒç®åŒãæããããããšãåŠã³ãŸãããããã®æ©èœã¯åžžã«å¹ççã«æ©èœãããšã¯éããŸããããã®ç¶æ³ã«ã¯ããã€ãã®çç±ããããŸãã第äžã«ãPythonã®äžè¬çãªå Žåã®ã³ãŒãã®åæã¯å°é£ã§ããããã¯ãããŒã¿åã¯ããã°ã©ã ã®å®è¡äžã«ãã远跡ã§ããªãããã§ãïŒãã®å Žåã¯ãããè¡ããŸãïŒã第äºã«ãååãšããŠãæé©åã§ã¯ãŸã PythonãçŽç²ã«ã³ã³ãã€ã«ãããèšèªã®é床ãéæã§ããªããããããã°ã©ã ãéåžžã«è¿
éã«åäœãããå¿
èŠãããå Žåã¯ãåçŽã«äœã¬ãã«èšèªã§èšè¿°ããããšããå§ãããŸãããã ããPythonã¯æè»ãªèšèªã§ãããããå¿
èŠã«å¿ããŠã€ã³ã¿ãŒããªã¿ãŒã«å¹²æžããããšãªããå€ãã®æé©åã¡ãœãããèªåã§å®è£
ã§ããŸãããã®ã©ã€ãã©ãªãšã以äžã«ãªã¹ããããŠããä»ã®ãããžã§ã¯ãã¯ããã®æ©äŒããã瀺ããŠããŸããUPDããããžã§ã¯ãã®èª¬æã¯ãSlideShareã®ãã¬ãŒã³ããŒã·ã§ã³ãšããŠãå©çšã§ããŸãããã¡ããã芧ãã ãã
以äžã«ãPythonã§ããã°ã©ã ã®å®è¡ãé«éåããããã®ããã€ãã®ä»ã®èå³æ·±ããããžã§ã¯ããžã®ãªã³ã¯ã瀺ããŸãã- PyPyã¯ãJITã³ã³ãã€ã«ããµããŒããã代æ¿ã®Pythonã€ã³ã¿ãŒããªã¿ãŒã§ããããã«ãããååãšããŠãPythonããã°ã©ã ãããé«éã«å®è¡ã§ããŸããPyPyèªäœã¯RPythonã§èšè¿°ãããŠãããCã³ãŒããžã®ãã©ã³ã¹ã¬ãŒã¿ãŒã¯ç¹å¥ã«èšèšãããŠããŸãã
- Pystonã¯ãã³ãŒããLLVMããŒã«ã䜿çšããŠæé©åããJITã³ã³ãã€ã«ã䜿çšããŠå®è¡ã§ããäžéLLVMè¡šçŸã«å€æãããæ°ãã代æ¿Pythonã€ã³ã¿ãŒããªã¿ãŒã§ãã
- Nuitka â Python. py2exe, *.exe-, , , Nuitka Python .
- WPython â CPython 2.6.4 -, .
- astoptimizer â , - .
- promise â , -, «». , , .
- foldy.py â , - ( constant folding ), , .
- , - ( constant binding ), .
UPDïŒ1ããŠãŒã¶ãŒããžãã¯ã¯ãããã€ãã®ãªã³ã¯ã瀺åããŸããã- NumPyã¯ïŒCã§èšè¿°ãããPythonã¢ãžã¥ãŒã«ãåããïŒã©ã€ãã©ãªã§ããã倧èŠæš¡ãªé
åãšè¡åã䜿çšããèšç®ãè¿
éã«å®è¡ã§ããã ãã§ãªããé«åºŠãªæ°åŠé¢æ°ã®åºç¯ãªã»ããã䜿çšã§ããŸãã
- Numbaã¯ãé
åã䜿çšããæ°åŠçèšç®ãšæŒç®ãå«ãããã°ã©ã ãé«éåããã©ã€ãã©ãªã§ããæé©åã¯ãã³ãŒãããã€ãã£ãCPUããã³GPUåœä»€ã«å€æããJITã³ã³ãã€ã«ïŒLLVMã䜿çšããå ŽåïŒãããã³ãã®ä»ã®å€å
žçãªæ¹æ³ã«ãããã®ã§ãã
- Numexprã¯ã察å¿ãããã€ãã³ãŒãã®åæãšå€æŽãéããŠæ°åŒã®èšç®ãé«éåããã©ã€ãã©ãªã§ããåŒã¯ããã€ãã®éšåã«åå²ã§ããŸãããã®äžéšã¯ãä»ã®éšåãããé »ç¹ã«åèšç®ããããã³ãŒãã®äžŠååãªã©ã«ãã£ãŠå éãããŸãã
- Cythonã¯Pythonèšèªã®ã¹ãŒããŒã»ããã®æé©åã³ã³ãã€ã©ã§ãããããã°ã©ã ã§éçåä»ãã䜿çšããCããã³C ++ã³ãŒããšå¯æ¥ã«ããåãããããšãã§ããŸãã
- JITã³ã³ãã€ã«ã«ããPythonã§ã®ããŸããŸãªèšç®ãé«éåããã©ã€ãã©ãªã®æŠèŠãå«ãèšäºã
UPDïŒ2ãDuneRatãŠãŒã¶ãŒã¯ãä»ã®ããã€ãã®Pythonã³ã³ãã€ã«ãããžã§ã¯ãã瀺ããŸããïŒshedskinãhopeãpythranã