casting int[] to bool[]

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Jan 29 09:25:31 PST 2009


On Thu, Jan 29, 2009 at 11:28 AM, Saaa <empty at needmail.com> wrote:
> Well casting it is then for me, for now.
>
> This way of reading all these booleans is a bit slow... a minute for 700_000
> lines
>
> Is there some way to speed this up?

It's probably due to the absurd amount of memory allocation you're
causing.  Each call to split() allocates an array (whose size is not
known in advance, so it requires several appends), and each call to
to!(int[]) allocates _another_ array.  That's 1.4 million arrays
you're allocating, most of which are garbage, and each allocation can
potentially trigger a GC cycle.

It would be a lot more efficient if you just parsed out the line yourself.

auto data = splitlines(read(`data\parsed.dat`).idup);
auto dataIn = new bool[][](data.length, 3);

foreach(i, line; data)
{
    dataIn[i][0] = line[14] == '1';
    dataIn[i][1] = line[16] == '1';
    dataIn[i][2] = line[18] == '1';
}

Now we perform only a few allocations, all of which are before the loop.


More information about the Digitalmars-d-learn mailing list