From: shevegen@... Date: 2019-02-06T13:11:16+00:00 Subject: [ruby-core:91431] [Ruby trunk Bug#15589] `Numeric#zero?` is much slower than `== 0` Issue #15589 has been updated by shevegen (Robert A. Heiler). I was reading the old link - 1998 is indeed a long time ago. :-) I don't want to write much about the .zero? versus .nonzero? method per se - I think that is up to matz to determine how useful the methods are or not. To me .zero? is intuitive and == 0 is understandable. I have no problem with either variant and I think it is a lot up to the personal/individual style. But let's leave that alone for the moment, and not look at .zero? or any other method names or such; sawa also specifically wrote about the difference in speed. I do not know how much work it would be to make .zero? as fast as == 0, but from a syntactic/semantic point of view, I think it would make sense to be able to treat both in the same way, speed-wise alone. At the least I don't see why they should have to be treated in a dissimilar manner, so I agree with sawa. If it can be changed, it would be great if it can be changed. But I think it may not have the highest priority either - but if possible then I think it would be an improve to have .zero? be as fast or as a comparable speed to == 0. (The syntax/semantic is of course for matz to have a look at; might be interesting to see if anything has changed since 1998 ... that's a LONG time by the way now with 2019 ... :) ) ---------------------------------------- Bug #15589: `Numeric#zero?` is much slower than `== 0` https://bugs.ruby-lang.org/issues/15589#change-76690 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal * Assignee: k0kubun (Takashi Kokubun) * Target version: * ruby -v: 2.6.1 * Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- 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. -- https://bugs.ruby-lang.org/ Unsubscribe: