c++ (gcc -O2 on Linux) Looking to speed up an array copy for loop with rotation

100 views Asked by At

I'm running a large ammt of data thru some code, and with profiling I've found two bottlenecks, the first I was able to almost completely eliminate by switching to memset as a means of initializing the arrays, but the second I can't figure out how to speed up.

The program takes as much as 11 hours to run sometimes, so any improvement would be important.

rows=new char[size];

//for(i=0;i<size;i++){
//  rows[i]='0';
//}
memset(rows,'0',size);
//using memset instead, cut the runtime contribution of that function from 20% down to 1%!!

call_lots_of_functions_not_included_here();

char *old_rows=rows;
rows=new char[xCount*yCount];
//    for(x=0;x<xCount;x++){
//      for(y=0;y<yCount;y++){
//        rows[y*xCount+(xCount-1-x)] = old_rows[x*yCount+y];  
//      }
//    }

int z=-1;
int i=-1;
for(x=0;x<xCount;x++){
 i=xCount-x-1;
 for(y=1;y<yCount;y++){
   i+=xCount;
   rows[i]=old_rows[z++];
 }
}

//By eliminating the y*xCount stuff, I was able to reduce the runtime to 1/5th original!
//But I can't help but think it can still be made faster... faaaster.
delete[] old_rows;

This for loop is at 26% of total program runtime, If I could figure out how to optimize it similar to the prior function, it'd be a big deal.

Note: looping thru the array and setting each char to '0' took 20% time, while looping thru the array and setting each char to be some specific value took 26% time, so the problem is NOT the array allocation, or deletion... there are simply a lot of characters being set.

note: example input vs output data (this function rotates a bitmap 90 degrees)
in     out     in_flattened
 0123   012    abcdefghijkl
0abcd  0iea    
1efgh  1jfb    out_flattened
2ijkl  2kgc    ieajfbkgclhd
       3lhd

Any ideas?

0

There are 0 answers