 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
zhaoyandong Guest
|
Posted: Sun Sep 18, 2005 11:20 pm Post subject: Is C faster than C++ |
|
|
In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
I'll appreciate any suggestion on this.
Thank you.
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Sun Sep 18, 2005 11:24 pm Post subject: Re: Is C faster than C++ |
|
|
zhaoyandong wrote:
| Quote: | In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
|
It isn't faster.
| Quote: | I'll appreciate any suggestion on this.
|
Go to different interviews.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Sep 18, 2005 11:28 pm Post subject: Re: Is C faster than C++ |
|
|
* zhaoyandong:
| Quote: | In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
I'll appreciate any suggestion on this.
|
Probably they wanted to check whether you understood the difference between C
and C++. And if you did, you would have answered that every C program can
also be expressed in C++ with just minor cleaning up of syntax and
declarations, and that very often the same compiler is used for C and C++. So
that given a fast C program, you also have a just as fast C++ program.
And then you might have gone on to talk about how some of that performance
could be traded for shorter development time and general maintainability, in
C++, but not in C.
And concluded that neither language is inherently faster than the other, but
that C++ gives you a much wider range of practical development techniques: all
of C, plus plus (ah, _that_'s what the "++" stands for!).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sun Sep 18, 2005 11:40 pm Post subject: Re: Is C faster than C++ |
|
|
zhaoyandong wrote:
| Quote: | In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
|
The question is like asking "which is faster, a Lamborghini or a
Ferrari". The answer should be, they're only as fast as it's driver
skill can make them go.
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Sun Sep 18, 2005 11:57 pm Post subject: Re: Is C faster than C++ |
|
|
zhaoyandong wrote:
| Quote: | In one of my interview, some people asked me why C is faster C++, and
tell me to illustrate at least two reasons.
|
It's a stupid question. C++ is mostly a superset of C. There's no reason why
a C program compiled by a C compiler should be faster than the same program
compiled by a C++ compiler. And if you use features that exist only in C++,
such as virtual functions, you would have to simulate the dynamic dispatch
in C somehow to do a comparison, and C shouldn't be able to do it any
faster. Or you might need a completely different design in C, and you can't
generalize about the performance comparison of functionally equivalent
programs of different design. The interviewers are either clueless about C++
or they expected you tell them they were talking nonsense.
DW
|
|
| Back to top |
|
 |
Dave Rahardja Guest
|
Posted: Mon Sep 19, 2005 3:57 am Post subject: Re: Is C faster than C++ |
|
|
On Mon, 19 Sep 2005 07:20:22 +0800, "zhaoyandong" <zhaoyandong (AT) sina (DOT) com.cn>
wrote:
| Quote: | In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
I'll appreciate any suggestion on this.
Thank you.
|
The idea that C is inherently faster than C++ is a myth, but one that I
encounter regularly, especially with ex-C programmers who don't really
understand the C++ language.
I'd go to a different interview if you want to use C++ in your daily work, or
else prepare to regularly fight an uphill battle against people who hold on to
that unfounded prejudice.
-dr
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Mon Sep 19, 2005 9:15 am Post subject: Re: Is C faster than C++ |
|
|
zhaoyandong wrote:
| Quote: | In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
I'll appreciate any suggestion on this.
Thank you.
|
Never be reluctant to ask for clarification or to disagree with the
premise of a question, particularly an interview question. After all,
the question may designed specifically to test for those traits. In
other words, how the candidate handles the loaded question is being
assessed and not the content of the answer given.
In this case, asking for clarification would be in order:
"By what measurement have you found C++ to be slower than C?"
If the response is "average dipatch time per function call." you could
then explain how virtual functions differ from directly called
routines. But you could also note that the overhead is minimal and is
rarely an issue.
Otherwise, feel free to question the premise of the question,,
particularly a loaded question:
"Not having any specific measurements, I would not be able
to conclude that C++ is slower than C."
Greg
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Mon Sep 19, 2005 9:42 am Post subject: Re: Is C faster than C++ |
|
|
Greg wrote:
| Quote: |
[snip]
In this case, asking for clarification would be in order:
"By what measurement have you found C++ to be slower than C?"
If the response is "average dipatch time per function call." you could
then explain how virtual functions differ from directly called
routines. But you could also note that the overhead is minimal and is
rarely an issue.
|
In fact it is *never* an issue, because it isn't slower. Not if you
compare a virtual function call with the *equivalent* functionality
in C. And that is all that matters: virtual function calls in a code
are there for a reason. So you need to satisfy that reason in C or C++.
When doing so, one figures out, that C++ virtual functions are the fastest
possible way to satisfy that reason. Plus there is one benefit: maintainability.
Other then that, I agree to everything else you said.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Walter Bright Guest
|
Posted: Mon Sep 19, 2005 9:42 am Post subject: Re: Is C faster than C++ |
|
|
"zhaoyandong" <zhaoyandong (AT) sina (DOT) com.cn> wrote
| Quote: | In one of my interview, some people asked me why C is faster C++, and tell
me to illustrate at least two reasons.
I can't find the answer in the web.
I'll appreciate any suggestion on this.
Thank you.
|
Tell them that performance bottlenecks are usually lurking in unanticipated
locations in the code, so you use a profiler and attempt to identify where
any bottlenecks are before wasting time optimizing the non-bottlenecks.
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Mon Sep 19, 2005 3:56 pm Post subject: Re: Is C faster than C++ |
|
|
Greg wrote:
....
| Quote: | If the response is "average dipatch time per function call." you could
then explain how virtual functions differ from directly called
routines. But you could also note that the overhead is minimal and is
rarely an issue.
|
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.
|
|
| Back to top |
|
 |
Ganesh Guest
|
Posted: Mon Sep 19, 2005 7:16 pm Post subject: Re: Is C faster than C++ |
|
|
C++ leads to a very generic code. But it may be the case that you do
not use such genericity always. For instance, you can have generic sort
function in C++. But if you always just use a specific 'quicksort on
doubles', may be a naive C or Fortran program written specifically for
this would be faster. My opinion is that C++ may introduce some hidden
costs, that may be visible only to an experienced programmer. Lower the
level of programming, (maybe) lower the overheads. This is IMHO.
|
|
| Back to top |
|
 |
Ganesh Guest
|
Posted: Mon Sep 19, 2005 7:37 pm Post subject: Re: Is C faster than C++ |
|
|
C++ leads to a very generic code. But it may be the case that you do
not use such genericity always. For instance, you can have generic sort
function in C++. But if you always just use a specific 'quicksort on
doubles', may be a naive C or Fortran program written specifically for
this would be faster. My opinion is that C++ may introduce some hidden
costs, that may be visible only to an experienced programmer. Lower the
level of programming, (maybe) lower the overheads. This is IMHO.
|
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Mon Sep 19, 2005 7:50 pm Post subject: Re: Is C faster than C++ |
|
|
Ganesh wrote:
| Quote: | C++ leads to a very generic code. But it may be the case that you do
not use such genericity always. For instance, you can have generic sort
function in C++. But if you always just use a specific 'quicksort on
doubles', may be a naive C or Fortran program written specifically for
this would be faster.
|
The sorting thing is a very poor example: A native C program would use qsort
and be slower since the call to the comparison function is not inlined
whereas the use of templates will allow the compiler to inline and optimize
the whole instantiation of sort for the particular type.
In order to actually beat C++ std::sort() you would have to roll your own
sorting code for, say, doubles in C. Now, when you do that, you could as
well just do it in C++ and provide a new sorting template. [Also, every
once in a while I try to beat the sorting and nth_element routines in the
STL of my compiler. It is *very* hard to just get even. I assure you:
implementing quick-sort for some built in data type is not going to beat
std::sort().]
| Quote: | My opinion is that C++ may introduce some hidden
costs, that may be visible only to an experienced programmer. Lower the
level of programming, (maybe) lower the overheads. This is IMHO.
|
In C++ you can program as low-level as you can in C. Also, not every level
of abstraction incurs overhead at run-time. Templates incur overhead at
compile time but can considerably improve performance during run time.
Best
Kai-Uwe Bux
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Mon Sep 19, 2005 8:47 pm Post subject: Re: Is C faster than C++ |
|
|
Ganesh wrote:
| Quote: | My opinion is that C++ may introduce some hidden costs, that may be
visible only to an experienced programmer. Lower the level of
programming, (maybe) lower the overheads. This is IMHO.
|
What overhead? I don't call overhead something so minimal that nobody but a
expert can measure it. And the cost of programming at a lower level is
usually very visible.
And by the way, there are no hidden costs. You can take the object code and
inspect it, supposed you have that experienced programmer at hand.
--
Salu2
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Tue Sep 20, 2005 1:34 am Post subject: Re: Is C faster than C++ |
|
|
Karl Heinz Buchegger wrote:
| Quote: | Greg wrote:
[snip]
In this case, asking for clarification would be in order:
"By what measurement have you found C++ to be slower than C?"
If the response is "average dipatch time per function call." you could
then explain how virtual functions differ from directly called
routines. But you could also note that the overhead is minimal and is
rarely an issue.
In fact it is *never* an issue, because it isn't slower. Not if you
compare a virtual function call with the *equivalent* functionality
in C. And that is all that matters: virtual function calls in a code
are there for a reason. So you need to satisfy that reason in C or C++.
When doing so, one figures out, that C++ virtual functions are the fastest
possible way to satisfy that reason. Plus there is one benefit: maintainability.
Other then that, I agree to everything else you said.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
I would agree that the difference in dispatch time between a virtual
function call and a direct call should not be an issue in a
well-written C++ program; but that statement is not the same as saying
the difference is always negligible in any C++ program, no matter how
it is written. Or that a C++ programmer need not be aware of the
difference.
For instance, one could imagine that adding a virtual method to a class
like std::string would have a measurable negative effect on the
performance of a C++ program with many stack-based strings. Now clearly
a virtual method in this case is no doubt a bad idea. But that fact may
not be readily apparent to a C programmer. In other words, a C++
programmer has to be aware that virtual function calls are not
completely free; and as a consequence, methods should not be declared
virtual indiscriminately.
C++ is obviously a more complex language than C. Greater complexity
does not necessarily imply less efficiency. But it does require more
care at times to recognize inefficiency when it arises. I believe this
is true if for no other reason than that there are simply more ways to
make such mistakes in C++. Now, just to be clear: I am not arguing that
C++ is too dangerous a language in which to write programs. On the
contrary - I'm not really stating anything other than the benefits of
C++ greater expressiveness cannot be realized in the absence of a solid
understanding of the language itself.
Greg
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|