A significant performance difference

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 1 03:51:58 PDT 2014


V Mon, 1 Sep 2014 12:38:52 +0300
ketmar via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
napsáno:

> 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. ;-)
> 

Yep, I found out something similar. with this ugly code

for (i = 0; i < W; ++i) {
		y = null;
		while (x.length) {

			foreach(lj, lp ; x) {
				j = lj;
				p = lp;
				x.remove(cast(int)j);
				break;
			}

			...
	}

The problem is with searching first element



More information about the Digitalmars-d-learn mailing list