Results 1 to 5 of 5

Thread: Cost of pure virtual

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Cost of pure virtual

    Hi guys,

    I'm in the area of high performance computing...and was wondering about the use of pure virtual vs non-virtual overhead cost.

    A little background: we use C++/Qt. We run our app on Windows/Linux using the usual compilers (Intel Compiler, Visual Studio, GCC). Our application is multithreaded. i.e. there's a bunch of Worker class executing various Tasks. Process() is where the bulk of latency will come from.

    Which design below is better?

    ====
    Virtual
    ====
    Qt Code:
    1. class Worker
    2. {
    3. public:
    4. void Do( Task* task )
    5. {
    6. task->Execute();
    7. }
    8. };
    9.  
    10. class Task
    11. {
    12. protected:
    13. virtual void Setup( int job )=0;
    14. virtual void Process( int job )=0;
    15. virtual void ReturnResult( int job )=0;
    16.  
    17. public:
    18. void Execute()
    19. {
    20. // ** Main loop is here **
    21. for( int job = 0; job < jobNum; job++ )
    22. {
    23. Setup( job ); // Virtual call
    24. Process( job ); // Virtual call
    25. ReturnResult( job ); // Virtual call
    26. }
    27. }
    28. };
    29.  
    30. class SpecificTask : public Task
    31. {
    32. protected:
    33. void Setup( int job ) { /* Specific processing code here */ }
    34. void ReturnResult( int job ) { /* Specific processing code here */ }
    35. };
    36.  
    37. class SubSpecificTask : public SpecificTask
    38. {
    39. public:
    40. void Process( int job ) { /* Specific processing code here */ }
    41. };
    To copy to clipboard, switch view to plain text mode 

    =========
    or Non-Virtual?
    =========
    Qt Code:
    1. class Worker
    2. {
    3. public:
    4. void Do( Task* task )
    5. {
    6. task->Execute();
    7. }
    8. };
    9.  
    10. class Task
    11. {
    12. public:
    13. void Execute()=0;
    14. };
    15.  
    16. class SpecificTask : public Task
    17. {
    18. protected:
    19. void Setup( int job ) { /* Specific processing code here */ }
    20. void ReturnResult( int job ) { /* Specific processing code here */ }
    21. };
    22.  
    23. class SubSpecificTask : public SpecificTask
    24. {
    25. public:
    26. void Process( int job ) { /* Specific processing code here */ }
    27. void Execute()
    28. {
    29. // ** Main loop is here **
    30. for( int job = 0; job < jobNum; job++ )
    31. {
    32. Setup( job ); // Base-class call
    33. Process( job ); // Non-virtual call
    34. ReturnResult( job ); // Base-class call
    35. }
    36. }
    37. };
    To copy to clipboard, switch view to plain text mode 

    Now, imo:

    1) Virtual. Better OO design: code reuse and more modular (i.e. Task, SpecificTask, SubSpecificTask all have well-defined functionality; in a sense it describes the program much better). The downside is 3 x jobNum virtual calls overhead inside Task::Execute().
    2) Non-virtual. Not as nice of a design and a lot of redundant coding needed for each of the new SubspecificTask (for loop, Setup, ReturnResult), but there's no virtual call in the main loop at all.

    What do you think? Does someone have a better idea?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cost of pure virtual

    Before you start reducing the function call overhead, first use a profiler to see what is the real bottleneck.

    What is the maximal value of jobNum?

  3. The following user says thank you to jacek for this useful post:

    ShaChris23 (4th November 2007)

  4. #3
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cost of pure virtual

    16384 is the maximum.

    I'm in the process of designing the class, so I cant profile it yet.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cost of pure virtual

    Donald E. Knuth once wrote "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil".

    The overhead will be barely noticeable and you can easily switch between those two solutions later, so I would go for the template method pattern.

  6. The following user says thank you to jacek for this useful post:

    ShaChris23 (4th November 2007)

  7. #5
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cost of pure virtual

    Jacek,

    Thanks for the advice. I've decided to go with the template solution.

    As a sidenote, I know that the Process() method's latency is something in the magnitude of milliseconds. So I really think with these modern compilers and processors, *hopefully* the cost of virtual methods should be minimal.

Similar Threads

  1. QPluginLoader not recognizing a plugin
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 29th June 2007, 14:13
  2. Compiling error
    By vfernandez in forum Newbie
    Replies: 2
    Last Post: 9th March 2007, 21:02
  3. inheritance, pure virtual
    By TheKedge in forum General Programming
    Replies: 2
    Last Post: 18th January 2007, 11:20

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.