ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: tomoyapenguin@gmail•com
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50427] [Ruby trunk Bug#14391] Integer#digitsが遅い
Date: Wed, 24 Jan 2018 12:58:02 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-14391.20180124125758.96613e8e776c9633@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14391.20180124125758@ruby-lang.org

Issue #14391 has been reported by tompng (tomoya ishida).

----------------------------------------
Bug #14391: Integer#digitsが遅い
https://bugs.ruby-lang.org/issues/14391

* Author: tompng (tomoya ishida)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
Integer#digitsが遅い

大きなIntegerのdigitsがto_sと比べてかなり遅い(計算量のオーダーが違う)ようです。

~~~ ruby
(9999**9999).to_s.chars.map(&:to_i).reverse # 0.030225秒
(9999**9999).digits # 1.187126秒 (40倍)

(99999**99999).to_s.chars.map(&:to_i).reverse # 1.888218秒
(99999**99999).digits # 195.594539秒 (100倍)
~~~

以下のようなアルゴリズムでパッチを作りました

~~~ ruby
# example for 123456789.digits
[123456789]                 # initial
[23456789, 1]               # divmod with 100000000
[6789, 2345, 1]             # divmod with 10000
[89, 67, 45, 23, 1]         # divmod with 100
[9, 8, 7, 6, 5, 4, 3, 2, 1] # divmod with 10, done
~~~

to_sと共通の処理を使う方が良いとは思ったのですがかなり大変そうだったのでそうはなっていない(digits専用のコードを書いての)パッチです


~~~ ruby
# ベンチマーク(変更前、変更後、to_sの速度比較)
[99**99, 999**999, 9999**9999, 99999**99999].each do |num|
  t=Time.now; num.to_s(10); puts Time.now-t
  t=Time.now; num.digits(10); puts Time.now-t
  puts
end
__END__
          99**99  999**999  9999**9999  99999**99999
to_s      0.00001 0.0001    0.013         1.850
patched   0.00005 0.0011    0.020         2.269
original  0.0003  0.009     1.252       211.126
~~~


---Files--------------------------------
digits_performance.patch (2.2 KB)


-- 
https://bugs.ruby-lang.org/

       reply	other threads:[~2018-01-24 12:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14391.20180124125758@ruby-lang.org>
2018-01-24 12:58 ` tomoyapenguin [this message]
2018-01-27  2:04 ` [ruby-dev:50437] [Ruby trunk Bug#14391] Integer#digitsが遅い akr
2018-01-27  2:24 ` [ruby-dev:50438] [Ruby trunk Bug#14391][Assigned] Integer#digitsが遅い akr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.issue-14391.20180124125758.96613e8e776c9633@ruby-lang.org \
    --to=ruby-dev@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).