[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: OpenStruct,, know what keys are set
From:       Aaron Smith <beingthexemplary () gmail ! com>
Date:       2007-03-10 15:53:15
Message-ID: 51120d0994d124de95bf5347995b4185 () ruby-forum ! com
[Download RAW message or body]

Thanks for all the input.

Heres some testing:

require 'ostruct'
require 'benchmark'

lady = OpenStruct.new
0.upto(100) do |i|
  eval("lady.cat#{i} = i")
  #puts eval("lady.cat#{i}")
end

Benchmark.bm do |x|
  x.report("Find all keys 1"){lady.methods(false).grep(/[^=]$/)}
  x.report("Find all keys 2"){lady.instance_variable_get("@table").keys}
  x.report("Find all keys 3"){lady.keys_added}
end

#puts "\n" + lady.methods(false).grep(/[^=]$/).to_s
#puts "\n" + lady.instance_variable_get("@table").keys.to_s
#puts "\n" + lady.keys_added #this is always nil.


OUTPUT:
                 user     system      total        real
Find all keys 1  1.130000   0.050000   1.180000 (  1.185657)
Find all keys 2  0.020000   0.000000   0.020000 (  0.019773)
Find all keys 3  0.000000   0.000000   0.000000 (  0.000020)

Results speak for themselves.

-- 
Posted via http://www.ruby-forum.com/.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic