From: Robert Klemme Date: 2005-10-14T00:21:54+09:00 Subject: Re: array1 + array2 newb question ------=_NextPart_000_002A_01C5D01A.1906F2A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit daz wrote: > Robert Klemme wrote: > A more significant disadvantage is that you both posted solutions > with equivalent results but for a slightly different problem than > the one posed by the OP ;) Right. Shouldn't make a difference performance wise though. >> That's why my solution just appends two element arrays to >> the collecting array. :-) > > ... And ends up 3rd out of 3 in the b/mk race :-D It depends... (see below) > IMHO - #inject is very useful but I don't see the advantage > of using it to hide an initial x = [] and > subjecting it to being flung round in circles. > > As you can see, it doesn't gain anything, here. > > (Not a squabble :-) Your benchmark version crashed on my system because it used too much mem. IMHO GC times should be *included* because in a real world situation the time overhead incurred by massive object allocations and deallocations does in fact influence performance. However, with a modified script I get 17:05:03 [source]: ruby /c/temp/ruby/mappings.rb Rehearsal ------------------------------------------ dz 5.719000 0.000000 5.719000 ( 5.733000) dzco 4.984000 0.000000 4.984000 ( 4.988000) sk 6.891000 0.000000 6.891000 ( 6.959000) co 6.187000 0.000000 6.187000 ( 6.193000) rk 5.625000 0.000000 5.625000 ( 5.674000) rk2 8.938000 0.000000 8.938000 ( 9.017000) rk3 9.000000 0.000000 9.000000 ( 9.074000) -------------------------------- total: 47.344000sec user system total real dz 5.719000 0.000000 5.719000 ( 5.762000) dzco 4.921000 0.000000 4.921000 ( 4.997000) sk 6.875000 0.000000 6.875000 ( 6.918000) co 6.234000 0.000000 6.234000 ( 6.270000) rk 5.641000 0.000000 5.641000 ( 5.669000) rk2 9.078000 0.000000 9.078000 ( 9.137000) rk3 9.078000 0.000000 9.078000 ( 9.118000) Interestingly the double inject performs so bad. However the one inject version is approx as fast as "dz". But the clear winner is *surprise surprise* dz = []; a2.each{|s| dz.concat a1.map {|y| [y, s]}} Would be interesting to see how timings differ for larger arrays... 17:15:43 [source]: ruby /c/temp/ruby/mappings-2.rb 96 Rehearsal ------------------------------------------ dz 3.375000 0.015000 3.390000 ( 3.405000) dzco 3.485000 0.000000 3.485000 ( 3.532000) sk 3.453000 0.000000 3.453000 ( 3.478000) co 3.532000 0.000000 3.532000 ( 3.581000) rk 3.984000 0.000000 3.984000 ( 4.030000) rk2 6.594000 0.000000 6.594000 ( 6.619000) rk3 6.640000 0.000000 6.640000 ( 6.663000) -------------------------------- total: 31.078000sec user system total real dz 3.344000 0.016000 3.360000 ( 3.370000) dzco 3.157000 0.000000 3.157000 ( 3.192000) sk 3.422000 0.000000 3.422000 ( 3.429000) co 3.203000 0.000000 3.203000 ( 3.240000) rk 3.266000 0.000000 3.266000 ( 3.256000) rk2 6.562000 0.000000 6.562000 ( 6.576000) rk3 7.031000 0.000000 7.031000 ( 7.117000) Differences are much larger. Still I have no clue why the double inject version is so slow... Kind regards robert ------=_NextPart_000_002A_01C5D01A.1906F2A0 Content-Type: application/octet-stream; name="mappings.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="mappings.rb" require 'benchmark'=0A= include Benchmark=0A= =0A= a1 =3D [1,2,3]=0A= a2 =3D %w{a b c}=0A= =0A= BMN =3D 100000 # <------ ?whatever?=0A= bmbm(7) do |bx|=0A= bx.report('dz') { BMN.times { dz =3D []; a2.each{|s| dz +=3D a1.map = {|y| [y, s]}} }}=0A= bx.report('dzco') { BMN.times { dz =3D []; a2.each{|s| dz.concat = a1.map {|y| [y, s]}} }}=0A= bx.report('sk') { BMN.times { sk =3D a2.inject([]){|s,x| s + a1.map = {|y| [y, x]}} }}=0A= bx.report('co') { BMN.times { sk =3D a2.inject([]){|s,x| s.concat = a1.map {|y| [y, x]}} }}=0A= bx.report('rk') { BMN.times { rk =3D a2.inject([]){|r,x| a1.each {|y| = r << [y,x]}; r} }}=0A= bx.report('rk2') { BMN.times { rk =3D a2.inject([]){|r,x| = a1.inject(r){|rr,y| rr << [y,x]}} }}=0A= bx.report('rk3') { BMN.times { rk =3D a2.inject([]){|r,x| = a1.inject(r){|r,y| r << [y,x]}} }}=0A= end=0A= =0A= ------=_NextPart_000_002A_01C5D01A.1906F2A0 Content-Type: application/octet-stream; name="mappings-2.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="mappings-2.rb" require 'benchmark' include Benchmark a1 =3D [1,2,3] a2 =3D %w{a b c} 5.times do a1 +=3D a1 a2 +=3D a2 end puts a1.length BMN =3D 100 # <------ ?whatever? bmbm(7) do |bx| bx.report('dz') { BMN.times { dz =3D []; a2.each{|s| dz +=3D a1.map = {|y| [y, s]}} }} bx.report('dzco') { BMN.times { dz =3D []; a2.each{|s| dz.concat = a1.map {|y| [y, s]}} }} bx.report('sk') { BMN.times { sk =3D a2.inject([]){|s,x| s + a1.map = {|y| [y, x]}} }} bx.report('co') { BMN.times { sk =3D a2.inject([]){|s,x| s.concat = a1.map {|y| [y, x]}} }} bx.report('rk') { BMN.times { rk =3D a2.inject([]){|r,x| a1.each {|y| = r << [y,x]}; r} }} bx.report('rk2') { BMN.times { rk =3D a2.inject([]){|r,x| = a1.inject(r){|rr,y| rr << [y,x]}} }} bx.report('rk3') { BMN.times { rk =3D a2.inject([]){|r,x| = a1.inject(r){|r,y| r << [y,x]}} }} end ------=_NextPart_000_002A_01C5D01A.1906F2A0--