Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Are function calls that costly? Need help optimizing

Are function calls that costly? Need help optimizing

Scheduled Pinned Locked Moved C / C++ / MFC
performancealgorithmshelptutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Budric B
    wrote on last edited by
    #1

    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

    L S T PJ ArendsP 4 Replies Last reply
    0
    • B Budric B

      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

      L Offline
      L Offline
      liquid_
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • B Budric B

        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

        S Offline
        S Offline
        squidev
        wrote on last edited by
        #3

        If you can step through your code, you could pin-point the costy call with the use of the @clk pseudo-register. In your watch window, add @clk @clk=0 When you step, the value for @clk will give you how long it took. Hope this helps:)

        1 Reply Last reply
        0
        • L liquid_

          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.

          B Offline
          B Offline
          Budric B
          wrote on last edited by
          #4

          Yes, I tried to use inline, that didn't help. The test was done in release mode.

          1 Reply Last reply
          0
          • B Budric B

            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

            T Offline
            T Offline
            Tom Archer
            wrote on last edited by
            #5

            Budric B. wrote: Any articles? CodeProject article on pseudoregisters (including @clk)[^]

            1 Reply Last reply
            0
            • B Budric B

              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

              PJ ArendsP Offline
              PJ ArendsP Offline
              PJ Arends
              wrote on last edited by
              #6

              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.

              Within you lies the power for good; Use it!

              B 1 Reply Last reply
              0
              • PJ ArendsP PJ Arends

                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.

                B Offline
                B Offline
                Budric B
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups