むらãŸã§ã™ï¼Žã“ã‚“ã°ã‚“ã‚.
[ruby-dev:31323] ã§æ出ã—ãŸãƒ‘ッãƒã«å¯¾ã—ã¦ï¼Œãƒ†ãƒ¼ãƒ–ル周辺ã®å‡¦ç†ã‚’変更ã—ãŸ
改良版をã“ã—らãˆã¾ã—ãŸï¼Žä¸€ã¤ç›®ã®æ·»ä»˜ãƒ‘ッãƒãŒãã‚Œã§ã™ï¼Žã“ã®å¤‰æ›´ã§ï¼Œ
ã„ãらã‹é«˜é€ŸåŒ–ã§ãã¾ã—ãŸã®ã§ï¼Œä»¥ä¸‹ã§å ±å‘Šã—ã¾ã™ï¼Ž
07/08/03 ã« Kenta M.[email protected] ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:
07/08/02 ã« Yusuke ENDOH[email protected] ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:
ã©ã†è¦‹ã¦ã‚‚é è—¤ã•ã‚“ã®å®Ÿè£…ã®ã»ã†ãŒå¹³å‡ã™ã‚‹ã¨é«˜é€Ÿã§ã‚るよã†ã«è¦‹ãˆã¾ã™ï¼Ž
ã„ãˆã€å¹³å‡ã™ã‚‹ã¨ã‚€ã‚‰ãŸã•ã‚“ã®æ–¹ãŒæ—©ã„ã§ã™ã€‚
ç§ã®å®Ÿè£…ãŒé è—¤ã•ã‚“ã®å®Ÿè£…よりも平å‡ã—ã¦é€Ÿã‹ã£ãŸç†ç”±ãŒåˆ†ã‹ã‚Šã¾ã—ãŸï¼Ž
é è—¤ã•ã‚“ã®å®Ÿè£…ã«ãŠã„ã¦ï¼Œbig2str_karatsuba() 関数ã®ã¯ã˜ã‚ã®æ–¹ã§
基数ベã‚を生æˆã—ã¦ã„るループãŒã‚ã‚Šã¾ã™ï¼Žãã®ä¸ã§ï¼Œå¿…ãšä¸€å›žã ã‘,
余分㪠bigsqr ãŒå‘¼ã³å‡ºã•ã‚Œã¦ã—ã¾ã„ã¾ã™ (çµæžœã¯æ¯”較ã§ä½¿ã†ã ã‘).
ã‚‚ã†ä¸€ç‚¹ï¼Žé è—¤ã•ã‚“ã®å®Ÿè£…ã§ã¯ï¼Œå¤‰æ›å¯¾è±¡ã®æ•°å€¤ã‚’,2^n ã§2ç‰åˆ†ã™ã‚‹ã“ã¨ã‚’
ç¹°ã‚Šè¿”ã—ã¦å‡¦ç†ã—ã¦ã„ã¾ã™ï¼Žåƒ•ã®å®Ÿè£…ã¯ã¨ã„ã†ã¨ï¼Œ2ç‰åˆ†ã§ã¯ãªã㦠2^n ã§
divmod ã—ãŸçµæžœã®å€¤ã«å¿œã˜ã¦åŸºæ•°ãƒ™ã‚ã®å¤§ãã•ã‚’変ãˆã¦ã„ã‚‹ã®ã§ï¼Œ
除算ã®çµæžœãŒæ€¥æ¿€ã«å°ã•ãªå€¤ã«ãªã£ãŸå ´åˆã«å†å¸°å‘¼ã³å‡ºã—ã®å›žæ•°ãŒå°‘ãªããªã‚Šã¾ã™ï¼Ž
ã“ã®é•ã„ãŒï¼Œå¹³å‡é€Ÿåº¦ã®å·®ã«ç¾ã‚Œã¦ã„るよã†ã«æ€ãˆã¾ã™ï¼Ž
å†å¸°ã‚ˆã‚Šã‚‚ループã®æ–¹ãŒåƒ…ã‹ãªãŒã‚‰è»½ã„ã¯ãšã§ã™ã‹ã‚‰ï¼Œå¾“ã£ã¦ï¼Œ
to_s ã•ã‚Œã‚‹å€¤ã®å†…部表ç¾ã«ã‚ˆã£ã¦ã¯ï¼Œåƒ•ã®å®Ÿè£…ã®æ–¹ãŒé…ããªã‚‹ã“ã¨ãŒã‚ã‚‹
ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“ (ãªã„ã‹ã‚‚ã—ã‚Œã¾ã›ã‚“).
特ã«ä¸€å›žç›®ã® to_s ãŒè‘—ã—ãé…ã„ã®ã§ã€ä½•ã‹ä¸‹æ‰‹ã‚’ã—ã¦ã„ã‚‹æ°—ãŒã—ã¾ã™ã€‚
é è—¤ã•ã‚“ã®å®Ÿè£…ã§åˆå›žã® to_s ãŒè‘—ã—ãé…ã„ç†ç”±ã¯ï¼Œä»Šã®ã¨ã“ã‚発見ã§ãã¦ã„ã¾ã›ã‚“ orz
蛇足ã§ã™ãŒã€ã‚€ã‚‰ãŸã•ã‚“版ã 㨠to_s(2) ã«å¤±æ•—ã™ã‚‹ã“ã¨ãŒã‚ã‚‹ã¿ãŸã„ã§ã™ã€‚
(snip)
ã“ã‚Œã¯ãƒ»ãƒ»ãƒ»æœ€åˆã®è¦å‘Šã¯ãã£ã¨åŸºæ•°ã®ãƒ™ã‚を求ã‚よã†ã¨ã—ãŸã¨ãã«ç™ºç”Ÿã—ã¦
ã‚‹ã‚‚ã®ã ã¨æ€ã„ã¾ã™ (ãã“ã—ã‹ãªã„ã§ã™ã—).
ãã®é€šã‚Šã§ã—ãŸï¼ŽåŸºæ•°ãŒå°ã•ã„ã¨ï¼Œrb_big_pow ã§ãƒ™ã‚を計算ã§ããªããªã‚‹
ã“ã¨ã‚’考ãˆã¦ã„ã¾ã›ã‚“ã§ã—ãŸï¼Žæ–°ã—ã„パッãƒã§ã¯ï¼Œé è—¤ã•ã‚“ã®å®Ÿè£…ã‚’å‚考ã«ï¼Œ
å°ã•ãªãƒ™ã‚ã‹ã‚‰ bigsqr を使ã£ã¦è¨ˆç®—ã™ã‚‹ã‚ˆã†ã«ã—ãŸã®ã§ï¼Œã“ã®å•é¡Œã¯è§£æ±ºã—ã¦ã„ã¾ã™ï¼Ž
ç§ãŒæ˜¨æ—¥æ出ã—ãŸãƒ‘ッãƒã¨ï¼Œ[ruby-dev:31323] ã§ç§ãŒæ出ã—ãŸãƒ‘ッãƒã®
速度をã¾ã 比較ã—ã¦ã„ã¾ã›ã‚“ã§ã—ãŸï¼Žã¨ã‚Šã‚ãˆãšï¼Œãã®æ¯”較çµæžœã‚’後ã»ã©
å ±å‘Šã—ãŸã„ã¨æ€ã„ã¾ã™ï¼Ž
ã§ï¼Œã“ã®ãƒ¡ãƒ¼ãƒ«ã«æ·»ä»˜ã—ãŸãƒ‘ッãƒã¨ï¼Œ[ruby-dev:31339] ã«æ·»ä»˜ã—ãŸãƒ‘ッãƒã®
速度を比較ã—ãŸçµæžœã‚’以下ã«ç¤ºã—ã¾ã™ï¼Žæ¯”較用ã«ä¸¡æ–¹ã®å¤‰æ›´ã‚’マージã—ãŸãƒ‘ッãƒã‚’
ï¼’ã¤ç›®ã®ãƒ•ã‚¡ã‚¤ãƒ«ã¨ã—ã¦æ·»ä»˜ã—ã¾ã—ãŸï¼Žã‚³ãƒ³ãƒ‘イルオプションã¯
CFLAGS=‘-O6 -mtune=pentium4 -march=i686’ ã¨ã—ã¦ã„ã¾ã™ï¼Ž
次ã®ã‚ˆã†ãªç°¡å˜ãªãƒ†ã‚¹ãƒˆã§ï¼Œç§ã®æ–°ã—ã„æ–¹ãŒå¹³å‡çš„ã«é€Ÿã„ã“ã¨ãŒåˆ†ã‹ã‚Šã¾ã—ãŸï¼Ž
$ ruby -e ‘100.times { puts
“(#{rand(50000)}**#{rand(50000)}).to_s(#{rand(35) + 2})” }’ >
test_bignum_endoh.rb
$ sed -e ‘s/to_s/to_s2/g’ test_bignum_endoh.rb > test_bignum_muraken.rb
$ time ./ruby test_bignum_endoh.rb > res.endoh.txt
real 11m7.579s
user 3m45.882s
sys 0m0.036s
$ time ./ruby test_bignum_muraken.rb > res.muraken.txt
real 10m14.448s
user 3m23.973s
sys 0m0.028s
今回ã®æ”¹å¤‰ä½œæ¥ä¸ã«ï¼Œbigdivmod,bigsqr,もã—ã㯠rb_big_mul0 ã®ã„ãšã‚Œã‹ã®
ãƒã‚°(?)を見ã¤ã‘ã¾ã—㟠(ã¾ã ç›´ã—ã¦ã¯ã„ã¾ã›ã‚“).
僕ãŒç¢ºèªã—ãŸç—‡çŠ¶ã¯ï¼Œbigdivmod ã®å¼•æ•° y ãŒæ£è¦åŒ–ã•ã‚Œã¦ã„ãªã„å ´åˆã«ï¼Œ
商 q ãŒä¸æ£ãªå€¤ã«ãªã‚Šã¾ã™ï¼Žå…·ä½“例を出ã™ã¨ï¼Œ
x = [3326864467, 3907086170, 971956897, 690519217, 3899134773,
3795824570, 442283194, 1330259551, 2003237075, 185150596,
3356760681, 1324341631, 2994369366, 4193523868, 856144703,
901986172, 819949625, 3949855622, 1036270654, 861377302,
1540531469, 1872568982, 2117697280, 1626080396, 914652449,
2236519255, 1632014126, 2752481805, 1004969631, 2885238682,
746945629, 2700918791, 1532651932, 15071 ]
y = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 ]
ã¨ãªã£ã¦ã„ã‚‹ (y ãŒæ£è¦åŒ–ã•ã‚Œã¦ã„ãªã„) ã¨ãã«å•† q ãŒä¸æ£ãªå€¤ã«ãªã‚Šã¾ã™ï¼Ž
剰余 r ã«ã¯å½±éŸ¿ã¯ã‚ã‚Šã¾ã›ã‚“ã§ã—ãŸï¼Ž
一ã¤ç›®ã®æ·»ä»˜ã®ãƒ‘ッãƒã‚’当ã¦ãŸã‚½ãƒ¼ã‚¹ã§ã“ã®ç—‡çŠ¶ãŒå‡ºã‚‹éƒ¨åˆ†ã¯ï¼Œ1101行目ã«ã‚ã‚‹
bigdivmod(x, b, &q, &r) ã¨ã„ã†å‘¼ã³å‡ºã—ã§ã™ï¼Žã“ã“ã§ï¼Œb ãŒæ£è¦åŒ–ã•ã‚Œã¦ã„ãªã„
ãŸã‚ã«ç—‡çŠ¶ãŒç™ºç”Ÿã—ãŸã‚ã‘ã§ã™ãŒï¼Œb 㯠rb_big_pow ã‹ bigsqr ã®ã©ã¡ã‚‰ã‹ã«ã‚ˆã£ã¦
生æˆã•ã‚Œã‚‹ã‚ªãƒ–ジェクトã§ã™ï¼Žä¸¡è€…ã®ã‚³ãƒ¼ãƒ‰ã‚’見るã¨ï¼Œbigsqr ã§å‘¼ã³å‡ºã—ã¦ã„ã‚‹
rb_big_mul0 ã®ä¸ã§ä½œã‚‰ã‚Œã‚‹ Bignum ㌠bigtrunc ã‚‚ã—ã㯠bignorm ã•ã‚Œãšã«
戻ã•ã‚Œã¦ã„ã‚‹ã®ã§ï¼Œã“ã‚ŒãŒåŽŸå› ã§ã¯ãªã„ã‹ã¨æ€ã„ã¾ã™ï¼Ž
ã‚‚ã£ã¨ã‚‚,bigdivmod ã‚’éžæ£è¦åŒ–オブジェクトã§ã‚‚æ£ã—ãå‹•ãよã†ã«ä¿®æ£ã™ã‚‹ã“ã¨ã§ã‚‚対応
出æ¥ã‚‹ã¨æ€ã„ã¾ã™ãŒï¼Œbigsqr ã®ä¸ã§ rb_big_mul0 を呼ã³å‡ºã—ã¦ã„ã‚‹ã¨ã“ã‚ã« bigtrunc
を被ã›ã‚‹ã ã‘ã§ä¿®æ£ã§ãã‚‹ã®ã§ï¼Œbigsqr ã‚’ã„ã˜ã‚‹ã»ã†ãŒç°¡å˜ã ã¨æ€ã„ã¾ã™
(もã—ã㯠rb_big_mul0 㧠z ã‚’ bigtrunc ã™ã‚‹ï¼‰ï¼Ž
ã©ã¡ã‚‰ã‚’è¡Œã†ã¹ãã‹ã¯ Bignum ã®ãƒ©ã‚¤ãƒ–ラリã¨ã—ã¦ã®ä»•æ§˜ã®å•é¡Œã ã¨æ€ã†ã®ã§ï¼Œ
最終的ã«ã¯ã¾ã¤ã‚‚ã¨ã•ã‚“ã®åˆ¤æ–ã«ãŠä»»ã›ã—ã¾ã™ï¼Ž