ruby/ruby — pull request #86 by krainboltgreene (2012-01-21 09:52:40+00:00)
Title: Cleaning up the initialize method, improving performance

- Using friendlier variable names
- Dropping `for..in` block for `#each`
- Inlining if statement

I used this to test:

``` ruby
require 'benchmark'
require 'forgery'

def new_ostruct_member(arg); end

def generate_data
(0..1000).map do |i|
{ "k#{i}" => "v#{i}" }
end.inject &:merge
end

@data = generate_data

oldt = Benchmark.realtime do
hash = @data.dup
@table = {}
if hash
for k,v in hash
@table[k.to_sym] = v
new_ostruct_member(k)
end
end
end

newt = Benchmark.realtime do
hash = @data.dup
@ntable ||= {}
hash.each do |key, value|
@ntable[key.to_sym] = value
new_ostruct_member key
end if hash
end

puts "Are both results equal? #{@table == @ntable}"
puts "New Open Struct #{((oldt / newt).round(4) * 100) - 100}% faster/slower"
```

General speed improvement: 60..90% faster