[LUGOS-PROG] Loop speedup

Andrej (Andy) Brodnik andrej.brodnik at imfm.uni-lj.si
Thu Jul 29 09:50:10 CEST 2004


On Wed, Jul 28, 2004 at 01:15:05PM +0200, Gregor Berginc wrote:
>
> ----------------------------
>
> double FeatureDistance::euclideanDistance(Feature *_f1, Feature *_f2) {
>     int fSize = _f1->getSize();
>     int *f1Value = _f1->getValue();
>     int *f2Value = _f2->getValue();
>
>     long dist = 0;
>     for (long i = 0, tmp = 0; i < fSize; i++) {
>         int d = (*f1Value) - (*f2Value);
>         dist += d * d;   /**** tole je problem *****/
>
> 	f1Value++;
> 	f2Value++;
>     } // for
>
>     return sqrt(dist);
> } // euclideanDistance
>
> ----------------------------
>
> Problem je, da je v tej izvedbi blazno pocasna. Gre namrec za to, da se
> ta rutina poklice ogromnokrat, zato je vsaka pohitritev bistvenega
> pomena. Se najbolj me jezi, da ce namesto dist v problematicni vrstici
> uporabim tmp (definiran na zacetku for zanke) stvar pade skoz: namesto
> 25s porabi 2.75s.

Morda je tmp v registru.

Kaj pa, ce bi naredil nekaj taksnega (ce ze zelimo delati aritmetiko z
naslovi):

----------------------------------------------------------------------
  register int* f1LastValue;
  register int* f1Value= polje;
  register int* f2Value= polje;
  register long dist= 0;
  register int tmp;

  f1LastValue= & (f1Value[1000]);

  while (f1Value <= f1LastValue) {
    tmp= (*f1Value++) - (*f2Value++);
    dist+= tmp*tmp;
  };
----------------------------------------------------------------------

in po ,,gcc -S -O4`` dobimo za zanko:

----------------------------------------------------------------------
.L5:
	movl (%ecx),%eax
	movl (%edx),%edi
	subl %eax,%edi
	movl %edi,%eax
	addl $4,%ecx
	addl $4,%edx
	imull %eax,%eax
	addl %eax,%ebx
	cmpl %esi,%edx
	jbe .L5
----------------------------------------------------------------------

LPA




More information about the lugos-prog mailing list