Thanks Jes=FAs!!

On Apr 8, 2011, at 9:00 AM, Jes=FAs Gabriel y Gal=E1n wrote:

> On Fri, Apr 8, 2011 at 8:48 AM, Vicente Bosch Campos =
<vbosch / gmail.com> wrote:
>> Hi,
>>=20
>> I am currently doing some code for a face detection algorithm. In =
order to generate the models I need to load 3 GB worth of data and do a =
K-means on it ( just to give some context of why I need all of the data =
loaded
>> at the same time).
>>=20
>> At some point of the code I am retrieving a line of the file, turning =
the items into floats and dividing them in different set of arrays.
>>=20
>> Once a line is extracted and given the correct format I add that =
array to the one containing all lines
>>=20
>> all_data +=3D line_array
>>=20
>> I have noticed that the + operation gets slower as the all_data array =
gets bigger but this should not be so at the end what the + operation is =
doing is inserting them at the back of the array and its always the same
>> amount hence it should always take the same time.
>=20
> The problem is that b +=3D array is the same as b =3D b + array, which
> creates a new array everytime, with the contents of both arrays. As b
> gets bigger, creating this intermediate array gets more and more
> expensive. You can use concat instead, so that you don't create
> intermediate arrays, just modify b:
>=20
> require 'benchmark'
>=20
> TIMES =3D 500
> Benchmark.bmbm do |x|
>  x.report("+=3D") { b =3D [];  TIMES.times {b+=3DArray.new(10000,1)}}
>  x.report("concat") { b =3D [];  TIMES.times =
{b.concat(Array.new(10000,1))}}
> end
>=20
>=20
>=20
> $ ruby bm_arrayconcat.rb
> Rehearsal ------------------------------------------
> +=3D      22.900000   3.500000  26.400000 ( 26.434147)
> concat   1.010000   0.020000   1.030000 (  1.038697)
> -------------------------------- total: 27.430000sec
>=20
>             user     system      total        real
> +=3D       5.090000   3.170000   8.260000 (  8.281054)
> concat   0.520000   0.010000   0.530000 (  0.533721)
>=20
> Hope this helps,
>=20
> Jesus.
>=20