Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / bench / bench_constantize.rb
blobf193e75a6c1bb4a233ff2592b47116ecb79d9749
2 def constantize_full(camel_cased_word)
3   unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
4     raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
5   end
7   Object.module_eval("::#{$1}", __FILE__, __LINE__)
8 end
10 def constantize_no_re(camel_cased_word)
11   Object.module_eval("::#{camel_cased_word}", __FILE__, __LINE__)
12 end
14 def constantize_no_eval(camel_cased_word)
15   unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
16     raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
17   end
18 end
20 require 'benchmark'
22 Integer(ARGV[0] || 10).times do 
23   Benchmark.bm(30) do |bm|
24     bm.report("constantize_full(\"Hash\")") { 500_000.times { constantize_full("Hash") }}
25     bm.report("constantize_no_re(\"Hash\")") { 500_000.times { constantize_no_re("Hash") }}
26     bm.report("constantize_no_eval(\"Hash\")") { 500_000.times { constantize_no_eval("Hash") }}
27   end
28 end