Take the 2008 Git User's Survey and help out! [ hide ]

public
Fork of wycats/merb-core
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/auser/merb-core.git
Search Repo:
merb-core / simple_benches / proc_eval_gsub.rb
100644 46 lines (37 sloc) 1.143 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'benchmark'
 
TIMES = (ARGV[0] || 100_000).to_i
 
hsh = {:controller => "foo", :action => "bar", :mime => "baz", :type => "bat"}
prock = proc {|hsh| "#{hsh[:controller]}/#{hsh[:action]}.#{hsh[:mime]}.#{hsh[:type]}" }
evall = "\"\#{hsh[:controller]}/\#{hsh[:action]}.\#{hsh[:mime]}.\#{hsh[:type]}\""
gsubb = ":controller/:action.:mime.:type"
 
def meth(hsh)
  "#{hsh[:controller]}/#{hsh[:action]}.#{hsh[:mime]}.#{hsh[:type]}"
end
 
Benchmark.bmbm do |x|
  x.report("proc") do
    TIMES.times do
      prock.call(hsh)
    end
  end
  
  x.report("eval") do
    TIMES.times do
      eval evall, binding, __FILE__, __LINE__
    end
  end
  
  x.report("gsub") do
    TIMES.times do
      gsubb.gsub(/(:controller|:action|:mime|:type)/) {|g| hsh[g[1..-1].to_sym] }
    end
  end
  
  x.report("meth") do
    TIMES.times do
      meth(hsh)
    end
  end
  
end
 
# TIMES == 100_000
# user system total real
# proc 0.350000 0.000000 0.350000 ( 0.346125)
# eval 1.400000 0.000000 1.400000 ( 1.407556)
# gsub 1.330000 0.000000 1.330000 ( 1.325826)
# meth 0.320000 0.000000 0.320000 ( 0.320849)