Gianni said:
Greg wrote:
...
Bzzt - wrong.
It is NOT a given that virtual calls are slower than non-virtual. On
some architectures, they may even be faster than regular function calls.
Performance is one of those things that depends on the situation at hand.
But it is not impossible either.
And remember the scenario. In response to our challenge, the
interviewer produces a huge stack of highly detailed profiling data
that conclusively shows that for the program profiled, calls dispatched
to virtual functions required more cycles than direct calls.
So what is the candidate to do now?
One approach would be to angrily denounce the data as a pack of lies,
while slamming his or her first on the table. This approach is a
surefire way to be hired if the intent of the question was to assess
the candidate's passion for C++. If that was not the intent of the
question, though, this technique may have mixed results.
The problem with the "angry denunciation" response though, is that the
data could very well be accurate. It cannot be dismissed.
Next, there is the approach that you are advocating: for the candidate
to argue that this data can be ignored since there are other programs
in which virtual function calls are not slower. But that response is
not well connected to the situation on hand. And it is of little
comfort here, if the program that the company sells happens not to be
one of those programs for which virtual function calls are faster than
direct calls. Again, the candidate appears to skirt the issue or be
unwilling to accept facts.
The best approach is head on: -Yes, there may be additional overhead
when calling a virtual function but that overhead is a good investment.
And here's why - and then rattle off the reasons.
Making excuses or saying that there may be exceptions in some cases is
simply "lame". It only makes the candidate look like an apologist.
Greg