Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason you are seeing a difference is <em>not</em> related to the implementation of <code>==</code> vs <code>eql?</code> but is due to the fact that Ruby optimizes operators (like <code>==</code>) to avoid going through the normal method lookup when possible.</p> <p>We can verify this in two ways:</p> <ul> <li><p>Create an alias for <code>==</code> and call that instead. You'll get similar results to <code>eql?</code> and thus slower results than <code>==</code>.</p></li> <li><p>Compare using <code>send :==</code> and <code>send :eql?</code> instead and you'll get similar timings; the speed difference disappears because Ruby will only use the optimization for direct calls to the operators, not with using <code>send</code> or <code>__send__</code>.</p></li> </ul> <p>Here's code that shows both:</p> <pre><code>require 'fruity' first = "Woooooha" second = "Woooooha" class String alias same_value? == end compare do with_operator { first == second } with_same_value { first.same_value? second } with_eql { first.eql? second } end compare do with_send_op { first.send :==, second } with_send_eql { first.send :eql?, second } end </code></pre> <p>Results:</p> <pre><code>with_operator is faster than with_same_value by 2x ± 0.1 with_same_value is similar to with_eql with_send_eql is similar to with_send_op </code></pre> <p>If you're the curious, the optimizations for operators are in <a href="https://github.com/ruby/ruby/blob/017f0ffe1822264a8ead114f0b6055805185c0ac/insns.def#L1581-1598" rel="nofollow"><code>insns.def</code></a>.</p> <p><em>Note</em>: this answer applies only to Ruby MRI, I would be surprised if there was a speed difference in JRuby / rubinius, for instance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload