Re: Is C faster than C++
"Dave Rahardja" <ask@me.com> wrote in message
news:tp7tj1ldqc pbercr2qa7m36gu qv66h4sf6@4ax.c om...
[color=blue]
> On Sat, 1 Oct 2005 06:55:03 -0400, "P.J. Plauger" <pjp@dinkumware .com>
> wrote:
>[color=green]
>>So if you declare *every* function either as extern "C" or with a
>>throw() qualifier, you can match the lower overhead of native C
>>code, at least with *some* compilers. But the least lapse in this
>>style -- such as writing C++ the way all the books tell you to --
>>and you get the bloat of exception handling whether or not you
>>use it.[/color]
>[color=green]
>>So if you don't use the library *designed* for use with Standard
>>C++, the way all the books tell you, you can match the lower overhead
>>of native C style, at least with *some* compilers.
>>
>>Now please review your words and consider whether there is anything
>>*inherently * inefficient with the language itself. Asymptotic behavior
>>doesn't mean much in the world of real programming.[/color]
>
> It's obvious that we're not going to convince each other in this matter.
> It
> appears that our views of what constitutes a fair comparison differ
> considerably.
>
> My experience with C++ in both the desktop and embedded worlds is that the
> overhead for _equivalent_ behavior is zero. Each overhead in C++ comes
> with an
> additional feature, which C does not afford you. C++ defaults to a larger
> set
> of features that you can "turn off", while C starts out with a basic set
> to
> which you must add. In my mind, a fair comparison involves turning off
> features in your C++ program to match the behavior of the C program that
> you're comparing it to. Anything else is not a fair comparison in my mind,
> but
> you obviously think that that's too much to ask in the "real world".[/color]
In the usual sense, yes. What's advertised for C++ is: if you don't
use it, you don't pay for it. What you admit above, and several others
have demonstrated, is that you have to "use" quite a bit of stuff from
exception handling to *not* pay for it. Specifically, you have to make
liberal use of throw specifications. (Or you can declare all of your
functions extern "C", don't use any C++ functions from the library,
and hence "turn off" cross-module type checking in the bargain.)
And BTW, not all implementations of Standard C++ eliminate all space
and time overheads for exceptions even if you do the above. It's
well over a decade now since compilers started shipping with exceptions,
and that's still the state of the art.
So yes, I do consider my comparisons fair, and yes, I think that's
too much to ask in the real world. Having said that, I observe that
some C++ compilers have a switch that disables exception handling
unilaterally. All you need then is a library that can tolerate
such antics. We happen to provide one called EC++. It's part of our
standard library package, alongside the Standard C++ library. But
the same people who argue that C++ has zero overheads usually foam
at the mouth at the mention of subsetting...
[color=blue]
> Maybe I can convince others, if not you, that what you consider
> "asymptotic
> behavior" is par for the course in many application domains, including
> mine
> (embedded code), although for most applications, the overhead associated
> with
> the "traditiona l" coding style is more than made up for by the features
> that
> C++ brings to the table.[/color]
Please note that I made exactly this point in my first posting.
My major point was that it's a mark of zealotry to deny that
overheads do exist.
[color=blue]
> However, C-equivalent efficiency is attainable by
> correct specification.[/color]
For some implementations . If you know what "correct" means.
And if you do your best to write C code masquerading as C++.
Please also note that this style is fragile. All you need is
one maintainer who's not clued in to the correct style to
add one innocent call (possibly copied from a reputable text
book) and suddenly bloat up an executable.
[color=blue]
> I stand by my statements and observations (and take advantage of them
> daily),
> although you probably disagree with them.[/color]
Actually, I don't.
[color=blue]
> I do concede that the two areas you highlight--default exception handling
> behavior and iostreams--contribute to significant data and code bloat in
> C++
> programs and must be aggressively managed for efficiency-critical code.[/color]
Yep. And it's the "aggressive ly managed" part that puts the lie
to the opinion that prompted my first posting:
: "Mabden" <mabden@sbc_glo bal.net> wrote in message
: news:q5b_e.640$ Y_5.493@newssvr 11.news.prodigy .com...
: ...
: Anything you believe about "hidden costs" are similar to your beliefs
: about UFOs and gods. Ie: non-existent.
The hidden costs are real if you have to work hard to drag
them out of hiding before you can exorcise them.
P.J. Plauger
Dinkumware, Ltd.
"Dave Rahardja" <ask@me.com> wrote in message
news:tp7tj1ldqc pbercr2qa7m36gu qv66h4sf6@4ax.c om...
[color=blue]
> On Sat, 1 Oct 2005 06:55:03 -0400, "P.J. Plauger" <pjp@dinkumware .com>
> wrote:
>[color=green]
>>So if you declare *every* function either as extern "C" or with a
>>throw() qualifier, you can match the lower overhead of native C
>>code, at least with *some* compilers. But the least lapse in this
>>style -- such as writing C++ the way all the books tell you to --
>>and you get the bloat of exception handling whether or not you
>>use it.[/color]
>[color=green]
>>So if you don't use the library *designed* for use with Standard
>>C++, the way all the books tell you, you can match the lower overhead
>>of native C style, at least with *some* compilers.
>>
>>Now please review your words and consider whether there is anything
>>*inherently * inefficient with the language itself. Asymptotic behavior
>>doesn't mean much in the world of real programming.[/color]
>
> It's obvious that we're not going to convince each other in this matter.
> It
> appears that our views of what constitutes a fair comparison differ
> considerably.
>
> My experience with C++ in both the desktop and embedded worlds is that the
> overhead for _equivalent_ behavior is zero. Each overhead in C++ comes
> with an
> additional feature, which C does not afford you. C++ defaults to a larger
> set
> of features that you can "turn off", while C starts out with a basic set
> to
> which you must add. In my mind, a fair comparison involves turning off
> features in your C++ program to match the behavior of the C program that
> you're comparing it to. Anything else is not a fair comparison in my mind,
> but
> you obviously think that that's too much to ask in the "real world".[/color]
In the usual sense, yes. What's advertised for C++ is: if you don't
use it, you don't pay for it. What you admit above, and several others
have demonstrated, is that you have to "use" quite a bit of stuff from
exception handling to *not* pay for it. Specifically, you have to make
liberal use of throw specifications. (Or you can declare all of your
functions extern "C", don't use any C++ functions from the library,
and hence "turn off" cross-module type checking in the bargain.)
And BTW, not all implementations of Standard C++ eliminate all space
and time overheads for exceptions even if you do the above. It's
well over a decade now since compilers started shipping with exceptions,
and that's still the state of the art.
So yes, I do consider my comparisons fair, and yes, I think that's
too much to ask in the real world. Having said that, I observe that
some C++ compilers have a switch that disables exception handling
unilaterally. All you need then is a library that can tolerate
such antics. We happen to provide one called EC++. It's part of our
standard library package, alongside the Standard C++ library. But
the same people who argue that C++ has zero overheads usually foam
at the mouth at the mention of subsetting...
[color=blue]
> Maybe I can convince others, if not you, that what you consider
> "asymptotic
> behavior" is par for the course in many application domains, including
> mine
> (embedded code), although for most applications, the overhead associated
> with
> the "traditiona l" coding style is more than made up for by the features
> that
> C++ brings to the table.[/color]
Please note that I made exactly this point in my first posting.
My major point was that it's a mark of zealotry to deny that
overheads do exist.
[color=blue]
> However, C-equivalent efficiency is attainable by
> correct specification.[/color]
For some implementations . If you know what "correct" means.
And if you do your best to write C code masquerading as C++.
Please also note that this style is fragile. All you need is
one maintainer who's not clued in to the correct style to
add one innocent call (possibly copied from a reputable text
book) and suddenly bloat up an executable.
[color=blue]
> I stand by my statements and observations (and take advantage of them
> daily),
> although you probably disagree with them.[/color]
Actually, I don't.
[color=blue]
> I do concede that the two areas you highlight--default exception handling
> behavior and iostreams--contribute to significant data and code bloat in
> C++
> programs and must be aggressively managed for efficiency-critical code.[/color]
Yep. And it's the "aggressive ly managed" part that puts the lie
to the opinion that prompted my first posting:
: "Mabden" <mabden@sbc_glo bal.net> wrote in message
: news:q5b_e.640$ Y_5.493@newssvr 11.news.prodigy .com...
: ...
: Anything you believe about "hidden costs" are similar to your beliefs
: about UFOs and gods. Ie: non-existent.
The hidden costs are real if you have to work hard to drag
them out of hiding before you can exorcise them.
P.J. Plauger
Dinkumware, Ltd.
Comment