1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib
Marc-Andre Lafortune 1866d483dc [ruby/prime] Optimize Integer#prime?
Miller Rabin algorithm can be used to test primality for integers smaller than a max value "MaxMR" (~3e24)

It can be much faster than previous implementation: ~100x faster for numbers with 13 digits, at least 5 orders of magnitude for even larger numbers (previous implementation is so slow that it requires more patience than I have for more precise estimate).

Miller Rabin test becomes faster than previous implementation at somewhere in the range 1e5-1e6. It seems that the range 62000..66000 is where Miller Rabin starts being always faster, so I picked 0xffff arbitrarily; before that, or above "MaxMR", the previous implementation remains.

I compared the `faster_prime` gem too. It is slower than previous implementation up to ~1e4. After that it becomes faster and faster compared to previous implementation, but is still slower than Miller Rabin starting at ~1e5 and up to MaxMR. Thus, after this commit, builtin `Integer#prime?` will be similar or faster than `faster_prime` up to "MaxMR".

Adapted from patch of Stephen Blackstone [Feature #16468]

Benchmark results and code: https://gist.github.com/marcandre/b263bdae488e76dabdda84daf73733b9

Co-authored-by: Stephen Blackstone <sblackstone@gmail.com>
2020-12-09 00:40:09 -05:00
..
benchmark Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
bundler Merge prepare version of Bundler 2.2.0 2020-12-08 17:30:02 +09:00
cgi Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
csv [ruby/csv] Add support for \r\n with skip_lines: /...$/ again 2020-11-24 09:33:55 +09:00
delegate Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
did_you_mean Remove experimental feaures and related code completely 2020-06-06 14:13:19 -04:00
drb Separate send into public_send and __send__ 2020-10-27 16:12:45 +09:00
forwardable [ruby/forwardable] Specify minimum Ruby version. 2020-12-03 17:35:29 +09:00
getoptlong Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
irb [ruby/irb] Add a fallback for check_code_block that does not depend on implementation-private APIs 2020-11-22 21:00:11 +09:00
logger [ruby/logger] Consider cygwin a Windows platform 2020-12-04 20:16:00 +09:00
matrix Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
net digest gem is available now 2020-12-03 19:17:29 +09:00
observer Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
optparse Added the missing licenses field to some default gems 2020-08-18 20:26:39 +09:00
ostruct Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
pstore Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
racc [ruby/racc] Bump version to 1.5.1 2020-11-10 21:35:04 +09:00
rdoc Backport cosmetic changes from upstream repo that is ruby/rdoc 2020-11-12 21:05:59 +09:00
reline [ruby/reline] Editing to initial content is not just cursor moving 2020-12-08 00:08:41 +09:00
rinda Added dependencies for rinda 2020-12-04 20:21:41 +09:00
rubygems Merge prepare version of RubyGems 3.2.0 2020-12-08 17:30:02 +09:00
singleton Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
timeout Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
tracer Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
unicode_normalize
uri Separate send into public_send and __send__ 2020-10-27 16:12:45 +09:00
weakref Added dependencies for weakref 2020-12-01 19:48:24 +09:00
webrick Revert https://github.com/ruby/webrick/pull/44 2020-11-13 12:06:42 +09:00
yaml Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
.document Bundler is not documented in RDoc, but something different 2020-04-07 13:01:19 +09:00
abbrev.gemspec Promote abbrev to default gems 2020-09-10 18:56:16 +09:00
abbrev.rb
base64.gemspec Promote base64 to default gems 2020-09-10 18:56:17 +09:00
base64.rb base64.rb: improve performance of Base64.urlsafe_encode64 2019-12-10 14:32:35 +09:00
benchmark.rb
bundler.rb Merge prepare version of Bundler 2.2.0 2020-12-08 17:30:02 +09:00
cgi.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
csv.rb [ruby/csv] Clarify and correct RDoc for converters (#178) 2020-11-24 09:33:55 +09:00
debug.gemspec Promote debug.rb to default gems. 2020-10-30 21:45:23 +09:00
debug.rb [Feature #16513] TracePoint#inspect returns "... file:line" (#3391) 2020-08-06 11:56:24 +09:00
delegate.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
did_you_mean.rb Sync did_you_mean 2020-06-06 13:02:08 -04:00
drb.rb
English.gemspec Added the missing licenses field to some default gems 2020-08-18 20:26:39 +09:00
English.rb
erb.gemspec Added dependencies for erb 2020-12-02 09:12:25 +09:00
erb.rb
fileutils.gemspec Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
fileutils.rb [ruby/fileutils] Make mkdir_p only attempt to create necessary directories 2020-12-04 19:50:26 +09:00
find.gemspec Promote find to default gems 2020-08-20 21:00:53 +09:00
find.rb
forwardable.rb Revert "Prefer #send over #__send__ when it is clear there is no possible conflict" 2020-11-05 20:54:34 +09:00
getoptlong.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
ipaddr.gemspec [ruby/ipaddr] fixup 90b46678a5b09ed32612b0629b7c51c6d39a0444 2020-12-03 17:48:32 +09:00
ipaddr.rb
irb.rb [ruby/irb] Stop using bang version for #inspect of result 2020-11-22 21:00:11 +09:00
logger.rb [ruby/logger] Mention File::Null in Logger#new document 2020-06-11 14:22:32 +09:00
matrix.rb [ruby/matrix] Optimize ** 2020-12-05 00:56:58 -05:00
mkmf.rb Separate send into public_send and __send__ 2020-10-27 16:12:45 +09:00
mutex_m.gemspec Update the license for the default gems to dual licenses 2020-08-18 20:26:39 +09:00
mutex_m.rb [ruby/mutex_m] Fix Mutex_m#initialize when the super's initialize has kwargs 2020-12-04 19:53:16 +09:00
observer.rb Separate send into public_send and __send__ 2020-10-27 16:12:45 +09:00
open-uri.gemspec Added dependencies for open-uri 2020-12-01 19:48:24 +09:00
open-uri.rb Remove the deprecated override of Kernel#open in open-uri 2020-08-16 07:41:40 -07:00
open3.gemspec Manually picked 724bdb8e1d 2020-09-14 14:07:37 +09:00
open3.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
optionparser.rb
optparse.rb Separate send into public_send and __send__ 2020-10-27 16:12:45 +09:00
ostruct.rb [ruby/ostruct] Update version 2020-11-04 17:52:03 -05:00
pp.gemspec Added dependencies for pp 2020-12-02 09:12:25 +09:00
pp.rb Fix pp when passed a empty ruby2_keywords-flagged hash as array element 2020-01-22 10:27:02 -08:00
prettyprint.gemspec Promote prettyprint to default gems 2020-10-08 16:40:46 +09:00
prettyprint.rb Fix articles. 2020-04-11 02:32:23 +09:00
prime.gemspec Added dependencies for prime 2020-12-01 19:48:24 +09:00
prime.rb [ruby/prime] Optimize Integer#prime? 2020-12-09 00:40:09 -05:00
pstore.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
racc.rb
rdoc.rb Skip the initialization of XDG_DATA_HOME with Permission denied 2020-04-23 21:11:21 +09:00
readline.gemspec Fixed the invalid SPDX identifier on gemspec 2020-08-18 20:26:40 +09:00
readline.rb
reline.rb [ruby/reline] Support bracketed paste mode 2020-12-05 02:58:59 +09:00
resolv-replace.gemspec Promote resolv-replace to default gems 2020-09-11 22:01:08 +09:00
resolv-replace.rb
resolv.gemspec Promote resolv to default gems 2020-09-11 21:50:07 +09:00
resolv.rb fe80 should be case insensitive too 2020-11-09 16:16:30 +09:00
rubygems.rb Merge prepare version of RubyGems 3.2.0 2020-12-08 17:30:02 +09:00
securerandom.gemspec Promote securerandom to default gems 2020-09-11 21:15:25 +09:00
securerandom.rb Make SecureRandom support Ractor 2020-09-09 12:45:43 -07:00
set.gemspec Promote set to default gems 2020-08-20 12:39:11 +09:00
set.rb [ruby/set] Add Set#<=> 2020-12-04 19:24:22 +09:00
shellwords.gemspec Promote shellwords to default gems 2020-09-10 18:56:17 +09:00
shellwords.rb Fix the ArgumentError message in shellsplit 2020-06-16 23:23:13 +09:00
singleton.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
tempfile.gemspec Added the missing licenses field to some default gems 2020-08-18 20:26:39 +09:00
tempfile.rb [ruby/tempfile] Improve the documentation for Tempfile.create and recommend Tempfile.open instead 2020-10-05 19:17:25 +02:00
time.gemspec Added dependencies for time 2020-12-01 19:48:24 +09:00
time.rb [DOC] Mentioned that Time.parse method is not validator 2020-06-21 16:16:15 +09:00
timeout.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
tmpdir.gemspec Added dependencies for tmpdir 2020-12-02 09:12:25 +09:00
tmpdir.rb [ruby/tmpdir] Warn when environment variables skipped (fixes #2) 2020-09-14 18:22:37 +09:00
tracer.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
tsort.gemspec Promote tsort to default gems 2020-09-09 21:53:09 +09:00
tsort.rb
un.gemspec Promote un.rb to the default gems. It's preparation for 3.0.0-preview2. 2020-11-05 20:21:31 +09:00
un.rb Revert "Prefer #send over #__send__ when it is clear there is no possible conflict" 2020-11-05 20:54:34 +09:00
uri.rb [ruby/uri] Remove RCS keywords 2020-03-26 18:06:14 +09:00
weakref.rb Extract version number from the source 2020-07-30 19:03:18 +09:00
webrick.rb [webrick][DOC] Describe the stance of WEBrick about its security and utilization (#3457) 2020-08-26 14:28:05 +09:00
yaml.rb Remove unnecessary word from comment 2020-07-28 12:14:45 +09:00