Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8818: symtoproc.rb

File symtoproc.rb, 1.5 kB (added by lifofifo, 2 years ago)

Performance comparision with Ruby 1.9

Line 
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   # Integer
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   # Integer
33   x.report { n.times { messages.map{|m| m.id} } }
34   x.report { n.times { messages.map(&:id) } }
35 end
36
37 # lifo:~/labs/rubyedge/bin pratik$ ./ruby ~/labs/symtoproc.rb
38 # Before patching for 1.9.0
39 #       user     system      total        real
40 #   0.390000   0.010000   0.400000 (  0.404891)
41 #   1.010000   0.010000   1.020000 (  1.025648)
42 # After patching..
43 #       user     system      total        real
44 #   0.350000   0.000000   0.350000 (  0.355118)
45 #   0.390000   0.010000   0.400000 (  0.411593)
46
47 # lifo:~/labs/rubyedge/bin pratik$ ruby ~/labs/symtoproc.rb
48 # Defined to_proc for 1.8.6
49 # Before patching for 1.8.6
50 #       user     system      total        real
51 #   0.740000   0.020000   0.760000 (  0.787545)
52 #   5.240000   0.040000   5.280000 (  5.333900)
53 # After patching..
54 #       user     system      total        real
55 #   0.700000   0.000000   0.700000 (  0.713298)
56 #   1.690000   0.010000   1.700000 (  1.705961)