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 коммит произвёл GitHub
Родитель 7cf7e6c332
Коммит c6f439a6a8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 81 добавлений и 79 удалений

Просмотреть файл

@ -10,4 +10,20 @@ benchmark:
int.finite?
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

Просмотреть файл

@ -2159,31 +2159,6 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
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:
* num.abs2 -> real
@ -2255,19 +2230,6 @@ numeric_polar(VALUE self)
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:
* 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_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, "arg", 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, "rect", numeric_rect, 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, "angle", float_arg, 0);

Просмотреть файл

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

Просмотреть файл

@ -2059,30 +2059,6 @@ rb_rational_canonicalize(VALUE 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:
* flo.numerator -> integer
@ -2832,9 +2808,6 @@ Init_Rational(void)
rb_define_method(rb_cNumeric, "denominator", numeric_denominator, 0);
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, "denominator", rb_float_denominator, 0);