[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shift in C++

From: David Bateman
Subject: Re: Shift in C++
Date: Tue, 30 Sep 2003 18:10:50 +0200
User-agent: Mutt/1.3.28i

According to Przemek Klosowski <address@hidden> (on 09/30/03):
> Bottom line: see how much better you'd run with 
>     for (int i = 1; i < length; i++) { retval(i) = retval(i-1); }
> followed by retval(0)=retval(length-1) if you would want circular behaviour

Err, you trash retval(i-1) on the previous iteration, so the code above
will fill retval with retval(0). Its probably better to do something like

   tmp1 = retval(0);
   for (int i = 1; i < length; i++) { 
     tmp2 = retval(i); 
     retval(i) = tmp1;
     tmp1 = tmp2;
   retval(0) = tmp1;

Though ideally, you'd in fact block the algorithm something like

   tmp1 = retval(0)
   for (int i=0; i < length; i+=block_size) {
     // The line below fills a cache line of block_size long
     tmp2 = retval(i)
     for (int j=min(block_size-1,retval.length()-i*block_size-1); j > 0; j--)
       retval(i+j) = retval(i+j-1);
     retval(i) = tmp1;
     tmp2 = tmp1;

to avoid as much as possible the circular copying in tmp1 and tmp2. Of course
you have to know the value of block_size. Or at least not set it too large,
so as to be generic.


David Bateman                                address@hidden
Motorola CRM                                 +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax) 
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:
How to fund new projects:
Subscription information:

reply via email to

[Prev in Thread] Current Thread [Next in Thread]