| 1 |
require 'benchmark' |
|---|
| 2 |
|
|---|
| 3 |
n = 1000 |
|---|
| 4 |
s = Struct.new :id |
|---|
| 5 |
messages = [] |
|---|
| 6 |
n.times { messages << s.new(:id => rand(n)) } |
|---|
| 7 |
|
|---|
| 8 |
class Symbol |
|---|
| 9 |
puts "Defined to_proc for #{RUBY_VERSION}" |
|---|
| 10 |
def to_proc |
|---|
| 11 |
Proc.new { |*args| args.shift.__send__(self, *args) } |
|---|
| 12 |
end |
|---|
| 13 |
end if RUBY_VERSION < '1.9' |
|---|
| 14 |
|
|---|
| 15 |
puts "Before patching for #{RUBY_VERSION}\n" |
|---|
| 16 |
|
|---|
| 17 |
Benchmark.bm do |x| |
|---|
| 18 |
|
|---|
| 19 |
x.report { n.times { messages.map{|m| m.id} } } |
|---|
| 20 |
x.report { n.times { messages.map(&:id) } } |
|---|
| 21 |
end |
|---|
| 22 |
|
|---|
| 23 |
class Symbol |
|---|
| 24 |
def to_proc |
|---|
| 25 |
lambda{|obj| obj.send self} |
|---|
| 26 |
end |
|---|
| 27 |
end |
|---|
| 28 |
|
|---|
| 29 |
puts "After patching..\n" |
|---|
| 30 |
|
|---|
| 31 |
Benchmark.bm do |x| |
|---|
| 32 |
|
|---|
| 33 |
x.report { n.times { messages.map{|m| m.id} } } |
|---|
| 34 |
x.report { n.times { messages.map(&:id) } } |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|