Click here to Skip to main content
15,888,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Experts,

i have two for loop
C++
   for(y = Height; y--)
      {
        for(x = Width; x--)
         {
           im->setData(x, y, Getdata(x,y));
         }

      }

//definition for get and set data is as below

  T Getdata(unsigned int x, unsigned int y) const
   {
      return _Data[x * y];
   }
	
   void SetData(unsigned int x, unsigned int y, T data)
   {
      _Data[x + _width * y] = data;
   }


i want to optimize it further ,can somebody help me further...

What I have tried:

i read some where for(int j=Num;j--) is more efficient then for(int j=0;j
Posted
Comments
Kornfeld Eliyahu Peter 8-Feb-16 6:09am    
What kind of optimization you are looking for?
(there is no real difference between incremental and decremental loops in performance, but sometime the order is important)
tasumisra 8-Feb-16 6:34am    
hi Kornfeld,
i am looking to optimise it for speed,basically i want to create some kind of hash table where it trying to call GetData each time...
any help would be appriciated

Thanks,
Vikas
Stefan_Lang 8-Feb-16 8:22am    
This is not related to optimization, therefore I do not post this as solution, but I see some problems with your code:

0. The for loop syntax is incorrect, it requires three terms, not two.

1. GetData() looks like a member function and accesses the variable _Data, which looks like a member variable. But it is called like a global function. is there something missing?

2. in GetData() you use a different formula to determine an array index than in SetData(). It doesn't appear to make sense, since Getdata(1,8), Getdata(2,4), Getdata(4,2), and Getdata(8,1) would all return the same element, _Data[8] ! I suggest that in Getdata() you should use the same index expression as in Setdata().

3. There appear to be various alternate spellings of variables and member names (e. g. SetData vs. Setdata, _width vs. Width), but it isn't clear if these are meant to be different symbols or not.

Most of these issues wouldn't exist, if you at least tried to compile your code before posting it. it doesn't need to actually run, but these trivial errors make it unnecessarily hard for us to understand what you want to achieve.
Sergey Alexandrovich Kryukov 8-Feb-16 10:27am    
Exactly. To optimize something, one needs something which works. The question simply makes no sense.
—SA
tasumisra 8-Feb-16 11:03am    
Hello Stefan,
this is pseudo code and i was only expecting indicative solution\design..

but as you pointed out few things i ll try to look into this.

Thanks

There are two possible general optimisations:

  1. Replace the function calls with the necessary code,
  2. Pre-calculate intermediate results.

The resulting code may look like this:
C++
int z = Width * (Height - 1);
for (y = Height - 1; y >= 0; y--, z-= Width)
{
    for (x = Width - 1; x >= 0; x--)
    {
        dest[x + z] = src[x * y];
    }
}
 
Share this answer
 
Comments
nv3 8-Feb-16 15:23pm    
My 5.
If I were you I'd get the code working and consistent before messing about with optimising anything. Your Set and Gets don't access the same objects for example. Once you do that (and if _Data is an array like thingy) how about using std::copy to actually do the copy? If that's not fast enough and T is a POD look at using std::memcpy.

But fix your code first otherwise it'll be fast and wrong.
 
Share this answer
 
Comments
tasumisra 8-Feb-16 6:32am    
Basically this is a pseudo code where emphasis is on eliminating a for loop by creating some kind of lookup....
Aescleal 8-Feb-16 6:39am    
In that case don't mess about with explicit loops, use std::copy or memcpy if that's not fast enough.
tasumisra 8-Feb-16 6:42am    
Thanks Aescleal,I will try this option.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900