casting int[] to bool[]

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Jan 29 11:40:19 PST 2009


On Thu, Jan 29, 2009 at 1:57 PM, Saaa <empty at needmail.com> wrote:

> Yay massive speedup!! (~2 sec for the loop)
> Is `bool = x == y` faster than `if ( x == y ) true else false` ?

No, just shorter to type.  They compile down to pretty much the same
machine code.

> For the 60 large boolean part I used a loop.
> Not that I really need the extra speedup (if any) but how could I manually
> unroll that loop?
> I know you can do things like that in D..

Oh, so you mean you have 60 bools on each line?  You can do that with
some templating and string mixin magic:

// declare these at global scope
template Tuple(T...) { alias T Tuple; }

// Generates a tuple of numbers in the range [0, n)
template Range(int n)
{
    static if(n == 0)
        alias Tuple!() Range;
    else
        alias Tuple!(Range!(n - 1), n - 1) Range;
}

// then this is your loop in main
foreach(i, line; data)
{
    // this foreach loop is run at compile time
    foreach(j; Range!(60))
        mixin("dataIn[i][" ~ j.stringof ~
            "] = line[14 + 2 * " ~ j.stringof ~ "] == '1';");
}


More information about the Digitalmars-d-learn mailing list