Vincent Fourmond wrote:
> > a) Why do you need it to be so fast? Two comparisons seems pretty
> > lightweight to me!
>
>   You forget function lookup: there is three function lookup for this
> code. Hardcoded in C is way faster (with only one function lookup).

I actually hadn't forgotten it, but (mistakenly) thought that < and >
didn't have normal method overhead. So, the spaceship operator is
actually more terse and faster:

class Numeric
  def sign1; self < 1 ? -1 : self > 1 ? 1 : 0; end
  def sign2; self<=>0; end
  def sign3; 0; end
end

require 'benchmark'

N = 1_000_000
Benchmark.bmbm{ |x|
  x.report{
    N.times{ -5.sign1; 5.sign1; 0.sign1 }
  }
  x.report{
    N.times{ -5.sign2; 5.sign2; 0.sign2 }
  }
  x.report{
    N.times{ -5.sign3; 5.sign3; 0.sign3 }
  }
  x.report{
    N.times{ -5.abs; 5.abs; 0.abs }
  }
}

Rehearsal ------------------------------------
   3.391000   0.000000   3.391000 (  3.421000)
   2.296000   0.000000   2.296000 (  2.313000)
   1.610000   0.000000   1.610000 (  1.609000)
   0.969000   0.000000   0.969000 (  0.969000)
--------------------------- total: 8.266000sec

       user     system      total        real
   2.781000   0.000000   2.781000 (  2.797000)
   2.250000   0.000000   2.250000 (  2.249000)
   1.547000   0.000000   1.547000 (  1.547000)
   0.922000   0.000000   0.922000 (  0.922000)

So, if we assume that it would be about as fast as #abs, then you're
talking about saving about 1.3 MICRO seconds per call (on a 3GHz P4). I
would be surprised (and interested) if saving that amount of time made
a difference in someone's application, for this particular method.

To put it in perspective:
Suppose your Ruby program were controlling a game or simulation.
Suppose you were getting 30fps. Suppose you were calling this method
10,000 times PER FRAME. The difference between having this method
rewritten in the core and using the spaceship operator gets you all the
way to...30.9fps.