1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test
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
..
-ext- Guard all accesses to RubyVM::MJIT with defined?(RubyVM::MJIT) && 2020-12-04 16:45:54 +01:00
base64
benchmark
bigdecimal
cgi
coverage
csv [ruby/csv] Add support for \r\n with skip_lines: /...$/ again 2020-11-24 09:33:55 +09:00
date
dbm
did_you_mean
digest
drb Guard all accesses to RubyVM::MJIT with defined?(RubyVM::MJIT) && 2020-12-04 16:45:54 +01:00
dtrace
erb
etc
excludes
fiber Add support for non-blocking Process.wait. 2020-12-09 08:55:35 +13:00
fiddle Revert "test/fiddle/helper.rb: remove duplication (#3863)" (#3865) 2020-12-08 21:58:08 +09:00
fileutils
gdbm
io
irb [ruby/irb] support more body argument for oneliner method definition 2020-11-22 21:00:11 +09:00
json SortedSet was removed at a3db08d7b6 2020-12-04 19:36:12 +09:00
lib Guard all accesses to RubyVM::MJIT with defined?(RubyVM::MJIT) && 2020-12-04 16:45:54 +01:00
logger [ruby/logger] Consider cygwin a Windows platform 2020-12-04 20:16:00 +09:00
matrix [ruby/matrix] Optimize ** 2020-12-05 00:56:58 -05:00
mkmf
monitor
net Guard all accesses to RubyVM::MJIT with defined?(RubyVM::MJIT) && 2020-12-04 16:45:54 +01:00
nkf
objspace
open-uri
openssl
optparse
ostruct
pathname
psych
racc
rdoc
readline
reline [ruby/reline] Editing to initial content is not just cursor moving 2020-12-08 00:08:41 +09:00
resolv
rinda Guard all accesses to RubyVM::MJIT with defined?(RubyVM::MJIT) && 2020-12-04 16:45:54 +01:00
ripper Store all kinds of syntax errors [Bug #17345] 2020-11-26 20:14:34 +09:00
ruby Add support for non-blocking Process.wait. 2020-12-09 08:55:35 +13:00
rubygems Merge prepare version of RubyGems 3.2.0 2020-12-08 17:30:02 +09:00
socket Revert getaddrinfo_a() 2020-12-07 13:33:53 +09:00
stringio
strscan
syslog
uri
webrick
win32ole
yaml
zlib [ruby/zlib] Add Zlib::Inflate#inflate :buffer keyword argument 2020-12-04 19:12:32 +09:00
runner.rb
test_abbrev.rb
test_delegate.rb
test_extlibs.rb
test_find.rb
test_forwardable.rb
test_ipaddr.rb
test_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
test_observer.rb
test_open3.rb
test_pp.rb
test_prettyprint.rb
test_prime.rb [ruby/prime] Optimize Integer#prime? 2020-12-09 00:40:09 -05:00
test_pstore.rb
test_pty.rb
test_rbconfig.rb
test_securerandom.rb
test_set.rb [ruby/set] Add Set#<=> 2020-12-04 19:24:22 +09:00
test_shellwords.rb
test_singleton.rb
test_syslog.rb
test_tempfile.rb
test_time.rb
test_timeout.rb
test_tmpdir.rb
test_tracer.rb
test_tsort.rb
test_unicode_normalize.rb
test_weakref.rb