Project

General

Profile

Actions

Feature #15589

closed

`Numeric#zero?` is much slower than `== 0`

Added by sawa (Tsuyoshi Sawada) almost 6 years ago. Updated over 4 years ago.

Status:
Closed
Target version:
-
[ruby-core:91420]
Tags:

Description

My understanding is that the predicate method Numeric#zero? is not only a shorthand for == 0, but is also optimized for frequent patterns. If zero? is not faster than == 0, then it loses its reason for existence.

However, According to benchmarks on my environment, number.zero? is around 1.23 times to 1.64 times slower than number == 0 when number is an Integer, Rational, or Complex. It is faster only when number is a Float.

And with number.nonzero?, it is even worse. It is about 1.88 times to 4.35 times slower than number != 0.

I think there is something wrong with this, and it should be possible to optimize these methods, which has somehow been missed.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0