diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-05-21 14:06:58 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-05-21 14:27:30 -0700 |
commit | 931ee4186b877856b212b0085cd7bd7f6a4aea67 (patch) | |
tree | 8d3fb3cd55415fbbb33bb4319ae0c0212dd24d8c /railties/RDOC_MAIN.rdoc | |
parent | da83a6f12690ac4569beb2155f15826e868fa7e2 (diff) | |
download | rails-931ee4186b877856b212b0085cd7bd7f6a4aea67.tar.gz rails-931ee4186b877856b212b0085cd7bd7f6a4aea67.tar.bz2 rails-931ee4186b877856b212b0085cd7bd7f6a4aea67.zip |
reduce object allocations
Example:
x = [1,2,3,4]
y = [3,2,1]
def test x, y
hash = {}
x.zip(y) { |k,v| hash[k] = v }
hash
end
def test2 x, y
Hash[x.zip(y)]
end
def test3 x, y
x.zip(y).each_with_object({}) { |(k,v),hash| hash[k] = v }
end
def stat num
start = GC.stat(:total_allocated_object)
num.times { yield }
total_obj_count = GC.stat(:total_allocated_object) - start
puts "#{total_obj_count / num} allocations per call"
end
stat(100) { test(x,y) }
stat(100) { test2(x,y) }
stat(100) { test3(x,y) }
__END__
2 allocations per call
7 allocations per call
8 allocations per call
Diffstat (limited to 'railties/RDOC_MAIN.rdoc')
0 files changed, 0 insertions, 0 deletions