From: Tom Sawyer Date: 2002-08-10T08:17:07+09:00 Subject: Re: ambiguity between local variable assignment and writter method i ran a little benchmark to see what kind of speed hit my solution would incur. it looks to be quite small, but there dosen't seem to be any way to be sure b/c i don't know how ruby works under the hood. but i gave it shot to at least get an idea of the worst case. (results below) require 'benchmark' include Benchmark n = 100000 def a1 a = 1 end def a2 a1 end def a3 end bm do |x| GC.start x.report("a1") { n.times { a1 } } GC.start x.report("a2") { n.times { a2 } } GC.start x.report("a=1") { n.times { a=1 } } GC.start x.report("a=1; a") { n.times { a=1; a } } GC.start x.report("a3") { n.times { a3 } } GC.start x.report("a3; a=1") { n.times { a3; a=1 } } end user system total real a1 0.080000 0.000000 0.080000 ( 0.087665) a2 0.120000 0.000000 0.120000 ( 0.123336) a=1 0.070000 0.000000 0.070000 ( 0.063004) a=1; a 0.080000 0.000000 0.080000 ( 0.083761) a3 0.060000 0.000000 0.060000 ( 0.076508) a3; a=1 0.080000 0.000000 0.080000 ( 0.111779) notice that the first call looks up a1 and the executes it. the second looks up a2, executes it, looksup a1, and executes it. so between the two the time it takes to do a method lookup (out of all the possible methods that might respond) will be approx 1/2 thier difference of .035671, thus .0178355. then the fourth report assigns a local variable, and the last assigns a local varibale and then looks it up. so the time to look up a local variable is their difference, .020757, and there's only one local variable in that scope! so we can already see that the its faster to look up methods then it is to lookup local variables (how does that happen?) finally the time it takes to do a local assignment is the difference of 5 and 6, approx .035271. about twice as long as a lookup. i'm not sure how ruby works under the hood, but given these results, i am starting to wonder if looking up local variables and looking up methods is done with the same lookup table. in which case, given that ruby might have to check this table before assignment, there would be practically NO performance loss. and if not, WORST CASE looks to be about 30% on local assignments. ummm.... thats about 4-5 months or moore's law. -- ~transami