From: masa@... Date: 2001-05-17T03:23:30+09:00 Subject: [ruby-talk:15305] Re: Discussion on new Ruby features >From: Christian Szegedy >Subject: [ruby-talk:15262] Re: Discussion on new Ruby features > The main application of such a feature would not be the string class, > but mathematical classes such as Matrix and Polynomial,... I considerd such an issue for NArray. > Or Take for example: > x = (a+b)*(c+d) > It could be mapped as (a.dup += b)*=(c.dup+=d), For NArray, this is written as; x = (a.dup.add!(b)).mul!(c.dup.add!(d)) But it does not always improve speed; % ruby bang.rb 1 calculating x = (a+b)*(c+d) ... Time: 3.09 sec % ruby bang.rb 2 calculating x = (a.dup.add!(b)).mul!(c.dup.add!(d)) ... Time: 3.47 sec % ruby bang.rb 3 calculating x = (a+b).mul!(c+d) ... Time: 2.85 sec -- bang.rb -- require "narray" REPEAT = 10 def bench_time(n=REPEAT) t1 = Time.times.utime for i in 1..n yield end t2 = Time.times.utime puts " Time: %.2f sec\n\n" % [t2-t1] end n = 1_000_000 a = NArray.float(n).indgen! b = NArray.float(n).indgen! c = NArray.float(n).indgen! d = NArray.float(n).indgen! case ARGV[0] when "1" print "calculating x = (a+b)*(c+d) ...\n" bench_time{ x = (a+b)*(c+d) } when "2" print "calculating x = (a.dup.add!(b)).mul!(c.dup.add!(d)) ...\n" bench_time{ x = (a.dup.add!(b)).mul!(c.dup.add!(d)) } when "3" print "calculating x = (a+b).mul!(c+d) ...\n" bench_time{ x = (a+b).mul!(c+d) } end -- Masa Tanaka