Results 1 to 9 of 9

Thread: performance - call function

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default performance - call function

    Qt Code:
    1. calc (int i) {
    2. pp = (i + val) * 200/s;
    3. .....................
    4. }
    5. for (int j=0; j<NUMBER; ++j) //NUMBER may be 1..n (typically from 1000 to 10000 (but it can grow up over 100 000)
    6. for (int i=0; i<NUMBER; ++i
    7. calc(i);
    To copy to clipboard, switch view to plain text mode 
    Hi, my question is: maybe is better unfolding calc function inside the 2nd for? (is there any problem with that call function?)
    thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: performance - call function

    If you only use the calc function inside of the loop, try declaring it
    Qt Code:
    1. static inline void calc(int i) {...}
    To copy to clipboard, switch view to plain text mode 
    This way the compiler will check to see if it can inline it into the loop..

    The static here is the "non-class or member" static, which means that the function is only visible in the compile-unit. Together with the inline it tells the compiler it can go ahead and do a lot of stuff (i.e. optimize) with the function it might otherwise not dare to do :-)

    If calc is a member-function it would not work this way of course. But you can make the function "inline"d non-the-less :-)

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: performance - call function

    sorry I don't understand the last part. calc is a menber function.....
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: performance - call function

    The keyword "static" has different meanings in C++, depending on where it is used.

    Since your calc is a member function, the meaning of static that I would use is not available to you. (Because adding static in from of a member function make the function a "static member function", not a "function with local visibility to the compilation unit")

    What you still could do is to make calc an inlined function...

    But if that does not work for you there is still a (rather hacky) method you could use...(not nice, but better than duplicating your code)
    Qt Code:
    1. //BEGIN: HEADER FILE
    2. class TestClass
    3. {
    4. public:
    5. TestClass() : pp(0), val(0), s(200) {}
    6. void calc(int i);
    7.  
    8. void containsLoop();
    9. private:
    10. int pp;
    11. int val;
    12. int s;
    13. };
    14. //END: HEADER FILE
    15.  
    16.  
    17.  
    18. //BEGIN: IMPLEMENTATION FILE
    19.  
    20. //You need to add all the member variables that you use as pass-by-reference parameters
    21. //The compiler will check if it makes sense to inline
    22. static inline void calcStaticImpl(const int i, int &pp, int &val, int &s)
    23. {
    24. pp = (i + val) * 200 / s;
    25. //etc
    26. }
    27.  
    28. void TestClass::calc(int i)
    29. {
    30. calcStaticImpl(i, pp, val, s);
    31. }
    32.  
    33. static const int NUMBER = 10000;
    34.  
    35. void TestClass::containsLoop()
    36. {
    37. for (int j = 0; j < NUMBER; ++j) {
    38. for (int i = 0; i < NUMBER; ++i) {
    39. calcStaticImpl(i, pp, val, s);
    40. }
    41. }
    42. }
    43.  
    44. int main(int argc, char* argv[])
    45. {
    46. TestClass test;
    47.  
    48. test.calc(1);
    49.  
    50. test.containsLoop();
    51.  
    52. return 0;
    53. }
    To copy to clipboard, switch view to plain text mode 

    But check first if that function call is really a problem for you. (Profilers are your friends ;-)
    Last edited by camel; 28th February 2007 at 15:10.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: performance - call function

    You can always convert the method to a macro.

  6. #6
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: performance - call function

    Quote Originally Posted by wysota View Post
    You can always convert the method to a macro.
    Yeah...but I like static inline better...it makes finding errors much much easier :-)

    And besides..according to the GCC manual An Inline Function is As Fast As a Macro

    And since this is one of the easier optimizations...I would imagine this is true for most compilers :-)

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: performance - call function

    But check first if that function call is really a problem for you.
    my question was this!!
    Regards

  8. #8
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: performance - call function

    Quote Originally Posted by mickey View Post
    my question was this!!
    Sure,

    and it is always a good idea to allow for inlining (hence the tips).

    It is always never a good idea to duplicate code (what I gathered you wanted to do).


    But I consider the "static inline" thing, not as really nice code, thus, you should check if the call itself is a problem. This is nothing we can answer without more information.

    Is the small calculation you have in there the only thing? => the overhead of the function call is probably noticeable...good idea to work towards inlining.

    Do you access perhaps a database? => The function call itself isn't your problem in this case, trying to have the compiler inline the function wouldn't do you any good, beside making your code uglier.

    Yes they are two extremes, but both would be possible from what I have seen...


    Take a look at a profiler of your choice and do an informed decision :-)

  9. #9
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: performance - call function

    By the way, in the case of the little test-programm I just posted, the decision would be easy (I made some small modifications):

    Do it, but only if you like the coding style, because it does not matter that much... 6.5% speed up (when you only have this one formula)... or to put it into perspective:
    10000000000 iterations in callLoop() in 245 seconds
    10000000000 iterations in inlineLoop() in 229 seconds
    This was done with an optimized build, i.e. "-O2 -march=opteron"


    10 Billion function calls will cost you. But if it is enough to warant uglier code at this point...I do not know...remember the more work you do inside of the function, the less the relative savings will be...

    For a pure debug build this looks as follows:
    10000000000 iterations in callLoop() in 441 seconds
    10000000000 iterations in inlineLoop() in 353 seconds
    As you see, here the difference is much more pronounced...but recompiling optimized is probably still a better option in this case ;-)
    Last edited by camel; 28th February 2007 at 22:49.

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 23:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 23:55
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 09:52
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.