GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jul-2008, 04:27
vmthiru vmthiru is offline
New Member
 
Join Date: Jul 2008
Posts: 1
vmthiru is on a distinguished road

Dynamic array value copy to another array


Hellow everybody

If any other efficient / (purpose of speed up) way to move 0,10,20,30....end values into another dynamic array:

CPP / C++ / C Code:
aValue = new float [10000*nValue];
m_Value = new float[(10000*nValue/10)];

for( int nSec = 0; nSec < 10000*nValue; nSec += 10 ) 
{
  int nLeftArray = nSec /10;
  m_Value[nLeftArray] = aValue[nSec];
}

Thanks advance
Last edited by LuciWiz : 02-Jul-2008 at 07:24. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Jul-2008, 06:53
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: dynamic array value copy to another array


Quote:
Originally Posted by vmthiru
If any other efficient / (purpose of speed up) way to move 0,10,20,30....end values into another dynamic array:
CPP / C++ / C Code:
aValue = new float [10000*nValue];
m_Value = new float[(10000*nValue/10)];

for( int nSec = 0; nSec < 10000*nValue; nSec += 10 ) 
{
  int nLeftArray = nSec /10;
  m_Value[nLeftArray] = aValue[nSec];
}
Although some compilers will reorder expressions which never vary in a loop as an optimization, you may want to perform the optimization manually:
CPP / C++ / C Code:
const int upperLimit = 10000*nValue;
for( int nSec = 0; nSec < upperLimit; nSec += 10 ) 
{
  int nLeftArray = nSec /10;
  m_Value[nLeftArray] = aValue[nSec];
}
If this code was to be maintained by anyone else, manually pulling the unchanging expression out of the loop will point out to the next person that you understood that recomputing the upper limit never changes throughout the life of the loop.
  #3  
Old 02-Jul-2008, 11:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,303
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: dynamic array value copy to another array


Quote:
Originally Posted by ocicat
Although some compilers will reorder expressions which never vary in a loop as an optimization, you may want to perform the optimization manually:
CPP / C++ / C Code:
const int upperLimit = 10000*nValue;
for( int nSec = 0; nSec < upperLimit; nSec += 10 ) 
{
  int nLeftArray = nSec /10;
  m_Value[nLeftArray] = aValue[nSec];
}
Furthermore it is possible (but not at all certain) that for some compilers and some CPUs, incrementing an integer is faster than dividing by 10, so you could even try something like
CPP / C++ / C Code:
    int nm, na;
    int upperlimit=10000*nValue;
    for(nm = 0, na=0; nm < upperlimit; nm++, na+=10) 
    {
        m_Value[nm] = aValue[na];
    }

To vmthiru:

Note that I have seen it stated as a certitude that using pointer notation is faster than array notation. That may or may not be true for some cases, but I have never seen it. I mean, I checked it out a couple of times, but I never saw any difference in performance. Not even with BDS C running under CP/M on my homebrew Z80 system in 1981). I have absolutely zero interest in pursuing this particular fable. (Your Mileage May Vary, but see Footnote.)

Now, in my experience, trying to outguess the compiler with regards to optimization may be an amusing divertissement, but it is rarely productive. Even if it proves to give some advantage for a particular compiler on a particular CPU with a particular set of optimization switches, it may very well be counter-productive for other compilers (and even for later versions of the same compiler).

For example I remember a case on an older machine with an older version of a particular compiler (and an older language), multiplication took seven times as many machine cycles as addition, and division took 14 times as many.

So, first of all, we would never divide if it were possible to perform the same arithmetic with multiplication.

Secondly, if we could add instead of multiply, we would do it.

So instead of
CPP / C++ / C Code:
  y = 4*x

We would write
CPP / C++ / C Code:
  y = x+x
  y = y+y

Nowadays, for integers, typical C compilers may recognize that multiplication of an integer by a power of 2 can be accomplished with a single shift instruction. At least that's what gcc does, even without turning on any optimization.

If the compiler is really good, it might even take our hand-optimized sequence and give code that is as good as just writing y=4*x in the first place. (Or, maybe not.)

Furthermore, to belabor the point, instead of multiplying by 10, we would do something like "multiply by four, add the original and then multiply by 2":
CPP / C++ / C Code:
  y = x+x
  y = y+y
  y = y+x
  y = y+y
As a matter of fact, I have used this little trick on embedded systems for processors that don't have a multiply instruction, so the actual multiplication takes many, many machine cycles.

I think a compiler for a more-capable CPU might be hard-pressed to make good optimization sense of my little "multiply by 10" sequence. (Of course, I could be wrong, maybe it could make it as efficient as if I had simply written y=10*x.)

Even if the compiler didn't optimize it for us, it is entirely likely that with instruction and data caching in modern CPUs, the actual machine time might not be favorably affected by any manual optimization efforts.

And, so it goes...

Bottom line (however, see Footnote): If someone shows you something on paper that is supposed to be faster, you can think about it and you can try it, but you don't have to believe it. If someone shows you some actual machine measurements, you should keep in mind that that only applies to the exact environment under which the tests were run (compiler, CPU, operating sytem, etc.)

Regards,

Dave

Footnote (the real bottom line): If you are actually trying to improve performance in a program (rather than just thinking, in general, about ways to speed things up), I think it's a good idea to find out where the real bottleneck is (by using a code profiler, for example). The real bottom line is that I have seen, time and time again, where people have spent resources (time==money) to optimize something that has a miniscule (at best) effect on overall performance. In other words, they often optimize the wrong thing.
Last edited by davekw7x : 02-Jul-2008 at 12:19.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 14:06
Copy text file into dynamic 2D array gj2007 C Programming Language 7 21-Nov-2007 20:50
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:24.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.