From: Rob Biedenharn Date: 2009-11-26T01:23:49+09:00 Subject: Re: Ruby internals & other questions On Nov 25, 2009, at 11:01 AM, Ralph Shnelvar wrote: > Is there a document or website that describes how Ruby works? Yes, plenty. You could start with ruby-lang.org or ask Google for JRuby, Rubinius, MacRuby, Maglev, or IronRuby and seek out the source code yourself. > > For instance ... > > > > y=0 > 1_000_000.times {|x| y+=x} > > > > (1) Does the block get compiled a million times? Well, "compiled" makes the answer tricky, but the answer is basically "no." It gets executed a million times, but the compilation or AST production will only happen once (ignoring what JRuby's JIT might do). > > (2) What's the best Ruby way to do a sum from 1 to 1_000_000 Use a formula and don't actually do a "sum". (Which is true in any language) > > (3) Is there a difference in speed between IRB.exe and ruby.exe in > executing the above code? Probably not significant. > > (4) In IRB, whats the best way to time the code, above? Benchmark it. (part of Ruby's standard library) -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com