A significant performance difference

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 1 02:38:52 PDT 2014


On Mon, 01 Sep 2014 09:22:50 +0000
bearophile via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> In theory the best solution is to improve the performance of the 
> "byKey.front" and "byValue.front" idioms.
i found that slowdown is from _aaRange(), not from delegates.
the following code is slow even w/o delegate creation:

    import core.stdc.stdio;

    ref K firstKey(T : V[K], V, K)(T aa) @property
    {
        return *cast(K*)_aaRangeFrontKey(_aaRange(cast(void*)aa));
    }

    ref V firstVal(T : V[K], V, K)(T aa) @property
    {
        return *cast(V*)_aaRangeFrontValue(_aaRange(cast(void*)aa));
    }


    int main() {
        long[int] aa;
        for (int i = 0; i < 50000; i++)
            aa[i] = i;

        long total = 0;

        while (aa.length) {
            //int k = aa.byKey.front;
            int k = aa.firstKey;
            //long v = aa.byValue.front;
            long v = aa.firstVal;
            aa.remove(k);
            total += k + v;
        }

        printf("%lld\n", total);
        return 0;
    }


seems that we need two more hooks for AAs: `_aaFrontKey()` and
`_aaFrontValue()`.

> x.pop() sounds nicer :-)
ah, sure. i'm not very good in inventing names. ;-)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140901/d28799ef/attachment.sig>


More information about the Digitalmars-d-learn mailing list