Results 1 to 9 of 9

Thread: performance - call function

Threaded View

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

    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 14:10.

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22: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, 08:52
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21: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
  •  
Qt is a trademark of The Qt Company.