1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Improve performance some Integer and Float methods [Feature #19085] (#6638)

* Improve some Integer and Float methods

* Using alias and Remove unnecessary code

* Remove commentout code
This commit is contained in:
S.H 2022-10-28 01:13:16 +09:00 committed by GitHub
parent 7cf7e6c332
commit c6f439a6a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2022-10-27 16:13:46 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>
4 changed files with 81 additions and 79 deletions

View file

@ -10,4 +10,20 @@ benchmark:
int.finite? int.finite?
infinite?: | infinite?: |
int.infinite? int.infinite?
integer_real: |
int.real
float_real: |
flo.real
integr_imag: |
int.imag
float_imag: |
flo.imag
integer_conj: |
int.conj
float_conj: |
flo.conj
integer_numerator: |
int.numerator
integer_denominator: |
int.denominator
loop_count: 20000000 loop_count: 20000000

View file

@ -2159,31 +2159,6 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
return nucomp_convert(klass, a1, a2, TRUE); return nucomp_convert(klass, a1, a2, TRUE);
} }
/*
* call-seq:
* num.real -> self
*
* Returns self.
*/
static VALUE
numeric_real(VALUE self)
{
return self;
}
/*
* call-seq:
* num.imag -> 0
* num.imaginary -> 0
*
* Returns zero.
*/
static VALUE
numeric_imag(VALUE self)
{
return INT2FIX(0);
}
/* /*
* call-seq: * call-seq:
* num.abs2 -> real * num.abs2 -> real
@ -2255,19 +2230,6 @@ numeric_polar(VALUE self)
return rb_assoc_new(abs, arg); return rb_assoc_new(abs, arg);
} }
/*
* call-seq:
* num.conj -> self
* num.conjugate -> self
*
* Returns self.
*/
static VALUE
numeric_conj(VALUE self)
{
return self;
}
/* /*
* call-seq: * call-seq:
* flo.arg -> 0 or float * flo.arg -> 0 or float
@ -2433,9 +2395,6 @@ Init_Complex(void)
rb_define_private_method(CLASS_OF(rb_cComplex), "convert", nucomp_s_convert, -1); rb_define_private_method(CLASS_OF(rb_cComplex), "convert", nucomp_s_convert, -1);
rb_define_method(rb_cNumeric, "real", numeric_real, 0);
rb_define_method(rb_cNumeric, "imaginary", numeric_imag, 0);
rb_define_method(rb_cNumeric, "imag", numeric_imag, 0);
rb_define_method(rb_cNumeric, "abs2", numeric_abs2, 0); rb_define_method(rb_cNumeric, "abs2", numeric_abs2, 0);
rb_define_method(rb_cNumeric, "arg", numeric_arg, 0); rb_define_method(rb_cNumeric, "arg", numeric_arg, 0);
rb_define_method(rb_cNumeric, "angle", numeric_arg, 0); rb_define_method(rb_cNumeric, "angle", numeric_arg, 0);
@ -2443,8 +2402,6 @@ Init_Complex(void)
rb_define_method(rb_cNumeric, "rectangular", numeric_rect, 0); rb_define_method(rb_cNumeric, "rectangular", numeric_rect, 0);
rb_define_method(rb_cNumeric, "rect", numeric_rect, 0); rb_define_method(rb_cNumeric, "rect", numeric_rect, 0);
rb_define_method(rb_cNumeric, "polar", numeric_polar, 0); rb_define_method(rb_cNumeric, "polar", numeric_polar, 0);
rb_define_method(rb_cNumeric, "conjugate", numeric_conj, 0);
rb_define_method(rb_cNumeric, "conj", numeric_conj, 0);
rb_define_method(rb_cFloat, "arg", float_arg, 0); rb_define_method(rb_cFloat, "arg", float_arg, 0);
rb_define_method(rb_cFloat, "angle", float_arg, 0); rb_define_method(rb_cFloat, "angle", float_arg, 0);

View file

@ -6,7 +6,17 @@ class Numeric
# Returns +true+ if +num+ is a real number (i.e. not Complex). # Returns +true+ if +num+ is a real number (i.e. not Complex).
# #
def real? def real?
return true true
end
#
# call-seq:
# num.real -> self
#
# Returns self.
#
def real
self
end end
# #
@ -19,7 +29,7 @@ class Numeric
# 1.integer? #=> true # 1.integer? #=> true
# #
def integer? def integer?
return false false
end end
# #
@ -29,7 +39,7 @@ class Numeric
# Returns +true+ if +num+ is a finite number, otherwise returns +false+. # Returns +true+ if +num+ is a finite number, otherwise returns +false+.
# #
def finite? def finite?
return true true
end end
# #
@ -40,8 +50,34 @@ class Numeric
# finite, <code>-Infinity</code>, or <code>+Infinity</code>. # finite, <code>-Infinity</code>, or <code>+Infinity</code>.
# #
def infinite? def infinite?
return nil nil
end end
#
# call-seq:
# num.imag -> 0
# num.imaginary -> 0
#
# Returns zero.
#
def imaginary
0
end
alias imag imaginary
#
# call-seq:
# num.conj -> self
# num.conjugate -> self
#
# Returns self.
#
def conjugate
self
end
alias conj conjugate
end end
class Integer class Integer
@ -146,7 +182,7 @@ class Integer
# #
# Since +int+ is already an Integer, this always returns +true+. # Since +int+ is already an Integer, this always returns +true+.
def integer? def integer?
return true true
end end
alias magnitude abs alias magnitude abs
@ -178,7 +214,7 @@ class Integer
# #
# For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9. # For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
def ord def ord
return self self
end end
# #
@ -208,7 +244,7 @@ class Integer
# #
# #to_int is an alias for #to_i. # #to_int is an alias for #to_i.
def to_i def to_i
return self self
end end
# call-seq: # call-seq:
@ -216,7 +252,7 @@ class Integer
# #
# Since +int+ is already an Integer, returns +self+. # Since +int+ is already an Integer, returns +self+.
def to_int def to_int
return self self
end end
# call-seq: # call-seq:
@ -244,6 +280,26 @@ class Integer
def ceildiv(other) def ceildiv(other)
-div(-other) -div(-other)
end end
#
# call-seq:
# int.numerator -> self
#
# Returns self.
#
def numerator
self
end
#
# call-seq:
# int.denominator -> 1
#
# Returns 1.
#
def denominator
1
end
end end
# call-seq: # call-seq:
@ -276,7 +332,7 @@ class Float
# Since +float+ is already a Float, returns +self+. # Since +float+ is already a Float, returns +self+.
# #
def to_f def to_f
return self self
end end
# #

View file

@ -2059,30 +2059,6 @@ rb_rational_canonicalize(VALUE x)
return x; return x;
} }
/*
* call-seq:
* int.numerator -> self
*
* Returns self.
*/
static VALUE
integer_numerator(VALUE self)
{
return self;
}
/*
* call-seq:
* int.denominator -> 1
*
* Returns 1.
*/
static VALUE
integer_denominator(VALUE self)
{
return INT2FIX(1);
}
/* /*
* call-seq: * call-seq:
* flo.numerator -> integer * flo.numerator -> integer
@ -2832,9 +2808,6 @@ Init_Rational(void)
rb_define_method(rb_cNumeric, "denominator", numeric_denominator, 0); rb_define_method(rb_cNumeric, "denominator", numeric_denominator, 0);
rb_define_method(rb_cNumeric, "quo", rb_numeric_quo, 1); rb_define_method(rb_cNumeric, "quo", rb_numeric_quo, 1);
rb_define_method(rb_cInteger, "numerator", integer_numerator, 0);
rb_define_method(rb_cInteger, "denominator", integer_denominator, 0);
rb_define_method(rb_cFloat, "numerator", rb_float_numerator, 0); rb_define_method(rb_cFloat, "numerator", rb_float_numerator, 0);
rb_define_method(rb_cFloat, "denominator", rb_float_denominator, 0); rb_define_method(rb_cFloat, "denominator", rb_float_denominator, 0);