"Patrick Hurley" <phurley / gmail.com> writes:

> On 2/20/06, George Ogata <g_ogata / optushome.com.au> wrote:
>> ... is a better solution.  So is using `defined?'
>> instead of `instance_variables.include?'.
>
> The problem with defined? is that it will only work in an eval
> context, that is if you use a block with define_method (which I
> personally prefer), then defined?(str_rep_of_var) is always an
> expression, so you need to use the instance_variables.include?

Well nothing's perfect... :-)

But from a performance perspective, defined? scales much better with
the number of instance variables hanging around.  Benchmarks reveal
that the balance point was at around 5 ivars for me.

require 'benchmark'

class C
  def initialize n
    n.times do |i|
      instance_variable_set("@x#{i}", i)
    end
    ivar = instance_variables[n/2]  # average-case for 'include?'
    Benchmark.bm do |b|
      b.report('defined?'){100000.times{ eval("defined?(@x0)")}             }
      b.report('include?'){100000.times{ instance_variables.include?(ivar)} }
    end
  end
end

(1..10).each do |i|
  puts " #{i} ".center(70, '=')
  C.new(i)
end

----------------------------------------------------------------------

================================= 1 ==================================
      user     system      total        real
defined?  0.340000   0.000000   0.340000 (  0.343841)
include?  0.130000   0.000000   0.130000 (  0.131621)
================================= 2 ==================================
      user     system      total        real
defined?  0.360000   0.000000   0.360000 (  0.355125)
include?  0.220000   0.000000   0.220000 (  0.229211)
================================= 3 ==================================
      user     system      total        real
defined?  0.390000   0.000000   0.390000 (  0.402486)
include?  0.240000   0.000000   0.240000 (  0.243938)
================================= 4 ==================================
      user     system      total        real
defined?  0.340000   0.000000   0.340000 (  0.347124)
include?  0.310000   0.000000   0.310000 (  0.304621)
================================= 5 ==================================
      user     system      total        real
defined?  0.350000   0.000000   0.350000 (  0.353857)
include?  0.340000   0.000000   0.340000 (  0.346883)
================================= 6 ==================================
      user     system      total        real
defined?  0.350000   0.000000   0.350000 (  0.344404)
include?  0.410000   0.000000   0.410000 (  0.409498)
================================= 7 ==================================
      user     system      total        real
defined?  0.370000   0.000000   0.370000 (  0.373150)
include?  0.490000   0.000000   0.490000 (  0.502982)
================================= 8 ==================================
      user     system      total        real
defined?  0.350000   0.000000   0.350000 (  0.352441)
include?  0.510000   0.000000   0.510000 (  0.515715)
================================= 9 ==================================
      user     system      total        real
defined?  0.350000   0.000000   0.350000 (  0.350805)
include?  0.560000   0.000000   0.560000 (  0.555990)
================================= 10 =================================
      user     system      total        real
defined?  0.350000   0.000000   0.350000 (  0.354339)
include?  0.650000   0.000000   0.650000 (  0.663272)