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
====
class Worker
{
public:
void Do( Task* task )
{
task->Execute();
}
};
class Task
{
protected:
virtual void Setup( int job )=0;
virtual void Process( int job )=0;
virtual void ReturnResult( int job )=0;
public:
void Execute()
{
// ** Main loop is here **
for( int job = 0; job < jobNum; job++ )
{
Setup( job ); // Virtual call
Process( job ); // Virtual call
ReturnResult( job ); // Virtual call
}
}
};
class SpecificTask : public Task
{
protected:
void Setup( int job ) { /* Specific processing code here */ }
void ReturnResult( int job ) { /* Specific processing code here */ }
};
class SubSpecificTask : public SpecificTask
{
public:
void Process( int job ) { /* Specific processing code here */ }
};
class Worker
{
public:
void Do( Task* task )
{
task->Execute();
}
};
class Task
{
protected:
virtual void Setup( int job )=0;
virtual void Process( int job )=0;
virtual void ReturnResult( int job )=0;
public:
void Execute()
{
// ** Main loop is here **
for( int job = 0; job < jobNum; job++ )
{
Setup( job ); // Virtual call
Process( job ); // Virtual call
ReturnResult( job ); // Virtual call
}
}
};
class SpecificTask : public Task
{
protected:
void Setup( int job ) { /* Specific processing code here */ }
void ReturnResult( int job ) { /* Specific processing code here */ }
};
class SubSpecificTask : public SpecificTask
{
public:
void Process( int job ) { /* Specific processing code here */ }
};
To copy to clipboard, switch view to plain text mode
=========
or Non-Virtual?
=========
class Worker
{
public:
void Do( Task* task )
{
task->Execute();
}
};
class Task
{
public:
void Execute()=0;
};
class SpecificTask : public Task
{
protected:
void Setup( int job ) { /* Specific processing code here */ }
void ReturnResult( int job ) { /* Specific processing code here */ }
};
class SubSpecificTask : public SpecificTask
{
public:
void Process( int job ) { /* Specific processing code here */ }
void Execute()
{
// ** Main loop is here **
for( int job = 0; job < jobNum; job++ )
{
Setup( job ); // Base-class call
Process( job ); // Non-virtual call
ReturnResult( job ); // Base-class call
}
}
};
class Worker
{
public:
void Do( Task* task )
{
task->Execute();
}
};
class Task
{
public:
void Execute()=0;
};
class SpecificTask : public Task
{
protected:
void Setup( int job ) { /* Specific processing code here */ }
void ReturnResult( int job ) { /* Specific processing code here */ }
};
class SubSpecificTask : public SpecificTask
{
public:
void Process( int job ) { /* Specific processing code here */ }
void Execute()
{
// ** Main loop is here **
for( int job = 0; job < jobNum; job++ )
{
Setup( job ); // Base-class call
Process( job ); // Non-virtual call
ReturnResult( job ); // Base-class call
}
}
};
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!
Bookmarks