From: Colin Bartlett Date: 2011-01-15T05:45:12+09:00 Subject: Re: Parallel Assignments and Elegance/Complexity Ratio. On Fri, Jan 14, 2011 at 4:41 PM, Charles Oliver Nutter wrote: > Parallel assignment is generally slower than straight-up assignment in > 1.9 and JRuby because it stands up a full Ruby Array for the RHS and > result of the entire assignment expression: I'd assumed it was something like that. Thanks for the explanation. > As you would expect this is a significant cost compared to just > assigning the values directly. JRuby can improve this when it knows > that the assignment is not being used as an expression: > ... > There are also some implementations that "cheat" (I mean that in the > nicest way possible) I don't know if you've heard of the English magician Paul Daniels, but I'm very fond of a phrase he uses to contrast himself with some people who are, let us say, not self-admitted magicians. "I cheat, but I cheat honestly". > and don't bother producing that array return value at all, > and they perform much better on parallel assignment as a result. Cheating honestly, I think! That's interesting, and the "cheating" idea hadn't ocurred to me. From the benchmarks below (similar to those you quoted; I did actually run the benchmarks twice, but the runs were very similar, so to avoid clutter I've only given one run) it seems MRC 1.9.1 is also cheating honestly if it can. Now the only thing that's puzzling me is why the MRI 1.9.1 "cheating honestly" version of parallel assignment seems to be slightly but clearly faster than the MRI 1.9.1 single assignment! require "benchmark" kt = 10_000_000 nn = 1 nn.times{ puts Benchmark.measure { kt.times {a,b,c=1,2,3 } } } nn.times{ puts Benchmark.measure { kt.times {a,b,c=1,2,3; nil} } } nn.times{ puts Benchmark.measure { kt.times {a = 1; b = 2; c = 3 } } } jruby 1.5.3 (ruby 1.8.7 patchlevel 249) (2010-09-28 7ca06d7) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java] 3.038000 0.000000 3.038000 ( 3.007000) 1.561000 0.000000 1.561000 ( 1.562000) 1.558000 0.000000 1.558000 ( 1.558000) ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32] 3.962000 0.000000 3.962000 ( 3.963000) 1.747000 0.000000 1.747000 ( 1.741000) 2.106000 0.000000 2.106000 ( 2.105000)