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

Ticket #8818: ruby_perform.rb

File ruby_perform.rb, 0.9 kB (added by lifofifo, 2 years ago)

Performance test for pure ruby code

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   def to_proc
10     Proc.new { |*args| args.shift.__send__(self, *args) }
11   end
12 end
13
14 puts "Before patching..\n"
15
16 Benchmark.bm do |x| 
17   # Integer
18   x.report { n.times { messages.map{|m| m.id} } }
19   x.report { n.times { messages.map(&:id) } }
20 end
21
22 class Symbol
23   def to_proc
24     lambda{|obj| obj.send self}
25   end
26 end
27
28 puts "After patching..\n"
29
30 Benchmark.bm do |x| 
31   # Integer
32   x.report { n.times { messages.map{|m| m.id} } }
33   x.report { n.times { messages.map(&:id) } }
34 end
35
36 # $ ruby ruby_perform.rb
37 # Before patching..
38 #       user     system      total        real
39 #   0.330000   0.010000   0.340000 (  0.335991)
40 #   1.750000   0.000000   1.750000 (  1.797727)
41 # After patching..
42 #       user     system      total        real
43 #   0.330000   0.010000   0.340000 (  0.332886)
44 #   0.430000   0.000000   0.430000 (  0.448419)