Are function calls that costly? Need help optimizing
-
Hi, I'm writing some simple image processing functions such as gaussian blur etc. I have an image class to represent the data. I then used the following to benchmark how fast it runs:
start = clock(); for (int k = 0; k < 100; k++) { //code } finish = clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC / 100;
I find that my GetPixel() function is the cause of very slow performance. For example here's the code that I can benchmark:for (int i = 0; i < test.m_nHeight; i++) { for (int j = 0; j < test.m_nWidth; j++) { int value = test.m_pData[i*test.m_nHeight + j]; //int value = test.GetPixel(i, j); } }
If I access the value directly from the pointer I get duration = 0.0013 s. The other commented line of code gives me 0.012. That's 10 times slower! I was hoping to wrap everything up nicely in a class, but it looks like that's not going to happen. Is it something I"m doing wrong, or just the way things are for function calls. Also are there any suggestions you have on code optimization? Any articles? I would not like to start writing messy C code all wrapped up in one function for speed. Thanks -
Hi, I'm writing some simple image processing functions such as gaussian blur etc. I have an image class to represent the data. I then used the following to benchmark how fast it runs:
start = clock(); for (int k = 0; k < 100; k++) { //code } finish = clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC / 100;
I find that my GetPixel() function is the cause of very slow performance. For example here's the code that I can benchmark:for (int i = 0; i < test.m_nHeight; i++) { for (int j = 0; j < test.m_nWidth; j++) { int value = test.m_pData[i*test.m_nHeight + j]; //int value = test.GetPixel(i, j); } }
If I access the value directly from the pointer I get duration = 0.0013 s. The other commented line of code gives me 0.012. That's 10 times slower! I was hoping to wrap everything up nicely in a class, but it looks like that's not going to happen. Is it something I"m doing wrong, or just the way things are for function calls. Also are there any suggestions you have on code optimization? Any articles? I would not like to start writing messy C code all wrapped up in one function for speed. ThanksTry to use
inline
function. Also, check the compiler options, eg. stack checking, optimizations... Do you build the release or debug version? The other way is to set on assembly listing in compiler options and compare different solutions of coding. Indeed, calling a function brings some overhead into your code. You can verify this examining mixed source and assembly listing, if compiler allows this (VC6 does allow). Generally, optimization can be a big problem and there is no perfect and only solution. -
Hi, I'm writing some simple image processing functions such as gaussian blur etc. I have an image class to represent the data. I then used the following to benchmark how fast it runs:
start = clock(); for (int k = 0; k < 100; k++) { //code } finish = clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC / 100;
I find that my GetPixel() function is the cause of very slow performance. For example here's the code that I can benchmark:for (int i = 0; i < test.m_nHeight; i++) { for (int j = 0; j < test.m_nWidth; j++) { int value = test.m_pData[i*test.m_nHeight + j]; //int value = test.GetPixel(i, j); } }
If I access the value directly from the pointer I get duration = 0.0013 s. The other commented line of code gives me 0.012. That's 10 times slower! I was hoping to wrap everything up nicely in a class, but it looks like that's not going to happen. Is it something I"m doing wrong, or just the way things are for function calls. Also are there any suggestions you have on code optimization? Any articles? I would not like to start writing messy C code all wrapped up in one function for speed. Thanks -
Try to use
inline
function. Also, check the compiler options, eg. stack checking, optimizations... Do you build the release or debug version? The other way is to set on assembly listing in compiler options and compare different solutions of coding. Indeed, calling a function brings some overhead into your code. You can verify this examining mixed source and assembly listing, if compiler allows this (VC6 does allow). Generally, optimization can be a big problem and there is no perfect and only solution. -
Hi, I'm writing some simple image processing functions such as gaussian blur etc. I have an image class to represent the data. I then used the following to benchmark how fast it runs:
start = clock(); for (int k = 0; k < 100; k++) { //code } finish = clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC / 100;
I find that my GetPixel() function is the cause of very slow performance. For example here's the code that I can benchmark:for (int i = 0; i < test.m_nHeight; i++) { for (int j = 0; j < test.m_nWidth; j++) { int value = test.m_pData[i*test.m_nHeight + j]; //int value = test.GetPixel(i, j); } }
If I access the value directly from the pointer I get duration = 0.0013 s. The other commented line of code gives me 0.012. That's 10 times slower! I was hoping to wrap everything up nicely in a class, but it looks like that's not going to happen. Is it something I"m doing wrong, or just the way things are for function calls. Also are there any suggestions you have on code optimization? Any articles? I would not like to start writing messy C code all wrapped up in one function for speed. ThanksBudric B. wrote: Any articles? CodeProject article on pseudoregisters (including @clk)[^]
-
Hi, I'm writing some simple image processing functions such as gaussian blur etc. I have an image class to represent the data. I then used the following to benchmark how fast it runs:
start = clock(); for (int k = 0; k < 100; k++) { //code } finish = clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC / 100;
I find that my GetPixel() function is the cause of very slow performance. For example here's the code that I can benchmark:for (int i = 0; i < test.m_nHeight; i++) { for (int j = 0; j < test.m_nWidth; j++) { int value = test.m_pData[i*test.m_nHeight + j]; //int value = test.GetPixel(i, j); } }
If I access the value directly from the pointer I get duration = 0.0013 s. The other commented line of code gives me 0.012. That's 10 times slower! I was hoping to wrap everything up nicely in a class, but it looks like that's not going to happen. Is it something I"m doing wrong, or just the way things are for function calls. Also are there any suggestions you have on code optimization? Any articles? I would not like to start writing messy C code all wrapped up in one function for speed. ThanksYou say your GetPixel() function. What are you doing in that function? The windows GetPixel() function is very slow to begin with, so if you are calling that one then it is no wonder your function is slow. That is why DIBSections were invented, so that one could access the pixels directly as you have done instead of relying on GetPixel(). Check out the Bitmaps section here on CP for some excellent DIBSection articles.
-
You say your GetPixel() function. What are you doing in that function? The windows GetPixel() function is very slow to begin with, so if you are calling that one then it is no wonder your function is slow. That is why DIBSections were invented, so that one could access the pixels directly as you have done instead of relying on GetPixel(). Check out the Bitmaps section here on CP for some excellent DIBSection articles.
My GetPixel() function is the same as the code I posted. It just dereferences the pointer. On top of that it checks if the indices are within limits and if not it returns a 0. I rewrote my gaussian blur algorithm to use the memory directly, without GetPixel() and now my results are 10 times faster. BTW. Thanks for the @clk tip.