Reputation: 5745
I have an exercise about optimization. I need to optimize a program which rotates and image by 45 degrees. I know accessing arrays using pointers is more efficient, so I tried the changes below- the original code:
RGB* nrgb = (RGB *)malloc(imgSizeXY*3);//3=sizeof(RGB)
//...
for (i=imgSizeY-1; i>=0; --i)
{
for (j=imgSizeX-1; j>=0; --j)
{
//...
int y=(i*imgSizeX+j);
nrgb[y].r = *imgInd; //*imgInd computed earlier
The changes:
RGB* nrgb = (RGB *)malloc(imgSizeXY*3);//3=sizeof(RGB)
RGB* rgbInd = nrgb+imgSizeXY-1;
for (i=imgSizeY-1; i>=0; --i)
{
for (j=imgSizeX-1; j>=0; --j)
{
rgbInd->r=*imgInd;
--rgbInd;
but when using pointers, the program produces an erroneous output. I have been staring at it for hours, and still have no idea why. Any ideas? Thank you very much!
Upvotes: 0
Views: 141
Reputation: 58271
An L1 cache hit is an order of magnitude faster than an L2 cache hit, which itself is an order of magnitude faster than a main memory access. See Numbers Every Computer Scientist Should Know. For image operations, you expect that you're going to have to do a lot of memory reads and writes, so you should expect to primarily be concerned with cache efficiency when optimising your code.
So concentrate on finding ways to use the caches more effectively, and don't worry too much that your compiler isn't optimising simple pointer arithmetic optimally.
Upvotes: 0
Reputation: 1102
There is no difference between access array elements by pointer and access by index. You can see that if produce assembler code. Index notatiin more simple.
Upvotes: 2