[前][次][番号順一覧][スレッド一覧]

ruby-changes:65299

From: S.H <ko1@a...>
Date: Sat, 20 Feb 2021 04:11:37 +0900 (JST)
Subject: [ruby-changes:65299] efd19badf4 (master): Improve performance some Numeric methods [Feature #17632] (#4190)

https://git.ruby-lang.org/ruby.git/commit/?id=efd19badf4

From efd19badf43f4f1f24d5aec8a28e94a6e1e47b5b Mon Sep 17 00:00:00 2001
From: "S.H" <gamelinks007@g...>
Date: Sat, 20 Feb 2021 04:11:19 +0900
Subject: Improve performance some Numeric methods [Feature #17632] (#4190)

---
 benchmark/numeric_methods.yml | 13 ++++++++++
 numeric.c                     | 58 -------------------------------------------
 numeric.rb                    | 46 ++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 58 deletions(-)
 create mode 100644 benchmark/numeric_methods.yml

diff --git a/benchmark/numeric_methods.yml b/benchmark/numeric_methods.yml
new file mode 100644
index 0000000..433c226
--- /dev/null
+++ b/benchmark/numeric_methods.yml
@@ -0,0 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/numeric_methods.yml#L1
+prelude: |
+  int = 42
+  flo = 4.2
+benchmark:
+  real?: |
+    int.real?
+  integer?: |
+    flo.integer?
+  finite?: |
+    int.finite?
+  infinite?: |
+    int.infinite?
+loop_count: 20000000
diff --git a/numeric.c b/numeric.c
index bcc0ab7..167eed6 100644
--- a/numeric.c
+++ b/numeric.c
@@ -712,35 +712,6 @@ num_divmod(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L712
 
 /*
  *  call-seq:
- *     num.real?  ->  true or false
- *
- *  Returns +true+ if +num+ is a real number (i.e. not Complex).
- */
-
-static VALUE
-num_real_p(VALUE num)
-{
-    return Qtrue;
-}
-
-/*
- *  call-seq:
- *     num.integer?  ->  true or false
- *
- *  Returns +true+ if +num+ is an Integer.
- *
- *      1.0.integer?   #=> false
- *      1.integer?     #=> true
- */
-
-static VALUE
-num_int_p(VALUE num)
-{
-    return Qfalse;
-}
-
-/*
- *  call-seq:
  *     num.abs        ->  numeric
  *     num.magnitude  ->  numeric
  *
@@ -826,31 +797,6 @@ num_nonzero_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L797
 
 /*
  *  call-seq:
- *     num.finite?  ->  true or false
- *
- *  Returns +true+ if +num+ is a finite number, otherwise returns +false+.
- */
-static VALUE
-num_finite_p(VALUE num)
-{
-    return Qtrue;
-}
-
-/*
- *  call-seq:
- *     num.infinite?  ->  -1, 1, or nil
- *
- *  Returns +nil+, -1, or 1 depending on whether the value is
- *  finite, <code>-Infinity</code>, or <code>+Infinity</code>.
- */
-static VALUE
-num_infinite_p(VALUE num)
-{
-    return Qnil;
-}
-
-/*
- *  call-seq:
  *     num.to_int  ->  integer
  *
  *  Invokes the child class's +to_i+ method to convert +num+ to an integer.
@@ -5430,12 +5376,8 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5376
     rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
     rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
 
-    rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
-    rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
     rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
     rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
-    rb_define_method(rb_cNumeric, "finite?", num_finite_p, 0);
-    rb_define_method(rb_cNumeric, "infinite?", num_infinite_p, 0);
 
     rb_define_method(rb_cNumeric, "floor", num_floor, -1);
     rb_define_method(rb_cNumeric, "ceil", num_ceil, -1);
diff --git a/numeric.rb b/numeric.rb
index 0746a83..0c191c0 100644
--- a/numeric.rb
+++ b/numeric.rb
@@ -1,3 +1,49 @@ https://github.com/ruby/ruby/blob/trunk/numeric.rb#L1
+class Numeric
+  #
+  #  call-seq:
+  #     num.real?  ->  true or false
+  #
+  #  Returns +true+ if +num+ is a real number (i.e. not Complex).
+  # 
+  def real?
+    return true
+  end
+
+  #
+  #  call-seq:
+  #     num.integer?  ->  true or false
+  #
+  #  Returns +true+ if +num+ is an Integer.
+  #
+  #      1.0.integer?   #=> false
+  #      1.integer?     #=> true
+  # 
+  def integer?
+    return false
+  end
+
+  #
+  #  call-seq:
+  #     num.finite?  ->  true or false
+  #
+  #  Returns +true+ if +num+ is a finite number, otherwise returns +false+.
+  #
+  def finite?
+    return true
+  end
+
+  #
+  #  call-seq:
+  #     num.infinite?  ->  -1, 1, or nil
+  #
+  #  Returns +nil+, -1, or 1 depending on whether the value is
+  #  finite, <code>-Infinity</code>, or <code>+Infinity</code>.
+  #
+  def infinite?
+    return nil
+  end
+end
+
 class Integer
   # call-seq:
   #    -int  ->  integer
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]