From: Michal Suchanek Date: 2007-12-29T01:37:10+09:00 Subject: Re: Matz says namespaces are too hard to implement - why? On 23/12/2007, Charles Oliver Nutter wrote: > So you're saying the stdlib delegator is slow? Do you have some > benchmarks for this? I'd like to examine it and see if I can improve it > or speed it up in JRuby. I believe there are parts of Rails that use > delegator too (or perhaps only used to), so it would be nice to see if > it's a real bottleneck. > I tried to benchmark some code using the delegator, and it turns out to be quite fast. So the problem was probably with actually using the methods for converting stuff or the methods stuck to the original class by the delegator. So we get a replacement of the stack-scoped namespace quite easily, and I would really like to see a usecase for stack-scoped namespace that cannot be implemented using a delegator. Thanks Michal user system total real UString 1.790000 0.100000 1.890000 ( 1.992710) Delegate 1.840000 0.090000 1.930000 ( 2.039103) Debug 1.810000 0.070000 1.880000 ( 1.984717) String 2.760000 0.090000 2.850000 ( 3.191666) user system total real UString 1.680000 0.100000 1.780000 ( 1.901071) Delegate 1.730000 0.100000 1.830000 ( 1.959464) Debug 1.790000 0.100000 1.890000 ( 2.284965) String 2.790000 0.130000 2.920000 ( 3.415921) user system total real UString 1.700000 0.100000 1.800000 ( 1.903906) Delegate 1.740000 0.100000 1.840000 ( 1.957677) Debug 1.770000 0.110000 1.880000 ( 1.975056) String 2.840000 0.110000 2.950000 ( 3.165277) # some utf-8 scattered tea in a string str="これはムルチバイトのストリング。このストリングに採番する事が出来ません。" require 'delegate' require 'benchmark' class UString < Array def self.from_str str return UString.new str.unpack("U*") end end class UStringDelegate < Array def self.from_str str return self.new str.unpack("U*").map{|x| SimpleDelegator.new x} end end class UChar < SimpleDelegator def inspect "'"+pack("U")+"'" end def to_s pack("U") end end class UStringDebug < Array def self.from_str str return self.new str.unpack("U*").map{|x| UChar.new x} end end 3.times {Benchmark.bm(10){|x| s1 = UString.from_str str x.report("UString"){ s2 = str[1..-2]; 100_0000.times{ s2 == s1[1..-2] }} s1 = UStringDelegate.from_str str x.report("Delegate"){ s2 = str[1..-2]; 100_0000.times{ s2 == s1[1..-2] }} s1 = UStringDebug.from_str str x.report("Debug"){ s2 = str[1..-2]; 100_0000.times{ s2 == s1[1..-2] }} s1 = str x.report("String"){ s2 = str[1..-2]; 100_0000.times{ s2 == s1[1..-2] }} }}