Generally, are the programs written by C++ slower than written by C10% ?

F

Fred Zwarts \(KVI\)

"KaiWen" wrote in message
Generally, are the programs written by C++ slower than written by C 10% ?

It is not clear to me what you mean.
C++ is more complicated than C, so C++ programs are written somewhat slower
that C programs, because more thinking is needed. But, at the other hand,
C++ usually needs less source code to complete a complex program (because of
the availability of a rich library and the reuse of code in templates), so
writing a program may be completed faster in C++ that in C.
 
A

A

It is not actually the language that might be slower because you can do
equal things in C and C++ that execute approximately the same speed, what
matters might be overhead that some C++ constructs imply on the program. But
that is only if you use such constructs... such as templates for example.
Even templates can also be used in a right and wrong way. So the answer is
far from being general. In good hands each tool can work wonders or be just
as average as any other tool.

For example:

std::vector<int> abc;
abc.at(0) = 1; // is slower than
abc[0] = 1; // than this because of range checking

In C you don't even have such constructs so there is nothing to compare it
to. But if you use at() which does some additional overhead due to range
checking then of course it is somewhat slower than the other version. It is
like you would call more functions in C, that would execute slower due to
heap/stack allocation etc.

But in C++ you could as well write:

char[] abc = "abc";
abc[0] = "x";

that would work equally fast in C or C++.

In general, there is only one REAL issue - which of the two you find more
valuable - the convenience that C++ offers over C to speedup development...
or... your time.... to finish a certain project (even if it is a bit
slower). Remember, programmers time is never getting more expensive it may
only get cheaper.
 
J

Joshua Maurice

"KaiWen"  wrote in message



It is not clear to me what you mean.
C++ is more complicated than C, so C++ programs are written somewhat slower
that C programs, because more thinking is needed. But, at the other hand,
C++ usually needs less source code to complete a complex program (becauseof
the availability of a rich library and the reuse of code in templates), so
writing a program may be completed faster in C++ that in C.

Do you have any reliable evidence to make such a claim? I believe this
is a complete pull out of thin air without any supporting evidence.
I'll add my own pull out of thin air and claim that this is a non-
obvious claim.

I'll go even further actually. As long as the C++ constructs can map
nicely to "equivalent" C constructs, I see no reason why C++ programs
will be slower with a good compiler. It depends heavily on the exact
programs. Usually people make unfair apples-to-oranges comparisons of
an OO approach vs a procedural approach. When doing legitimate
comparisons, you will find the C and C++ program are much closer than
you think.
 
K

KaiWen

Do you have any reliable evidence to make such a claim? I believe this
is a complete pull out of thin air without any supporting evidence.
I'll add my own pull out of thin air and claim that this is a non-
obvious claim.

I'll go even further actually. As long as the C++ constructs can map
nicely to "equivalent" C constructs, I see no reason why C++ programs
will be slower with a good compiler. It depends heavily on the exact
programs. Usually people make unfair apples-to-oranges comparisons of
an OO approach vs a procedural approach. When doing legitimate
comparisons, you will find the C and C++ program are much closer than
you think.

We don't discuss OO here, just discuss the cost of compile time and
the speed of program.
1. Compile a C++ program is slower than written by C
2. In runtime, the speed of the program written by C++ is <= the same
program written by C

I want to write a program about network, parse huge number of data,
so, with all due respect, I can not decide witch language to use.

I know I can develop faster by using C++, but the speed of program
in runtime is important too. So witch one is important? Can anyone
give a suggestion?
 
I

Ian Collins

I want to write a program about network, parse huge number of data,
so, with all due respect, I can not decide witch language to use.

I know I can develop faster by using C++, but the speed of program
in runtime is important too. So witch one is important? Can anyone
give a suggestion?

A poor choice of algorithm will run just as slow in well written C as in
well written C++.

A good choice of algorithm will run just as fast in well written C++
(and maybe faster if generic code such as sort is used) as in well
written C.
 
G

Goran

We don't discuss OO here, just discuss the cost of compile time and
the speed of program.
1. Compile a C++ program is slower than written by C
2. In runtime, the speed of the program written by C++ is <= the same
program written by C

For all practical intents and purposes, C++ is a subset of C.
Therefore, both these claims are theoretically false. ;-)

+1 for those who said that speed (and compile time, really) will
depend much, much more on your team's skills with C or C++ than on
some "inherent" difference between the two.

In other words, you're going about it in a wrong way.
I want to write a program about network, parse huge number of data,
so, with all due respect, I can not decide witch language to use.

Even worse. If that is so, then know this: network is orders of
magnitude "slower" than either C or C++. The time needed to establish
connections and transfer data between peers will easily swamp time
spent in your code.
I know I can develop faster by using C++, but the speed of program
in runtime is important too. So witch one is important? Can anyone
give a suggestion?

Best suggestion I can give from what you have said so far is: speed-
wise, it is, by and large, IRRELEVANT whether you pick C or C++ (And,
you may just as well go for, I dunno, Java and still not have a
relevant difference in speed.)

Goran.
 
R

Rui Maciel

KaiWen said:
We don't discuss OO here, just discuss the cost of compile time and
the speed of program.
1. Compile a C++ program is slower than written by C

How is this relevant?

2. In runtime, the speed of the program written by C++ is <= the same
program written by C

This, as I already pointed out, is false.

I want to write a program about network, parse huge number of data,
so, with all due respect, I can not decide witch language to use.

Possibly you are better served by using the language you are most proficient
with. Compilation time is practically irrelevant and speed efficiency
doesn't depend on which of these two languages you use, only on what code
you write.


Rui Maciel
 
N

Nick Keighley

For all practical intents and purposes, C++ is a subset of C.
seriously?

Therefore, both these claims are theoretically false. ;-)

even if C were a subset of C++ I don't see how the statment is
theoretically untrue
 
R

Rui Maciel

Goran said:
For all practical intents and purposes, C++ is a subset of C.

I don't believe that features such as support for object-oriented
programming, polymorphism, templates and namespaces can be excluded from
"practical intents".


Rui Maciel
 
G

Goran

even if C were a subset of C++ I don't see how the statment is
theoretically untrue

Whoops, lapsus linguae there... (blushes). I meant superset, of
course.

Goran.
 
B

BGB

How is this relevant?

depends on the tasks.

although not as likely to be an issue for statically compiled C vs C++
(except maybe for someone worried about build times, or people getting
bored and having a coffee break because building takes so long, ...) it
may not matter much.

in terms of, say, writing a compiler for a script language which is
often loaded-from-source (slower compile times thus meaning the app
takes longer to start up, ...), then having a fast compiler matters a
lot more.

This, as I already pointed out, is false.

actually, I think it can go either way (depending on programming styles
in each case, ...).

however, if in both cases it is the same code (common subset of C and
C++), with no "evil tricks" in the background (overloaded operators,
....), one wouldn't expect to find much real difference.

Possibly you are better served by using the language you are most proficient
with. Compilation time is practically irrelevant and speed efficiency
doesn't depend on which of these two languages you use, only on what code
you write.

yep.
 
J

Juha Nieminen

KaiWen said:
2. In runtime, the speed of the program written by C++ is <= the same
program written by C

Actually many equivalent constructs in C and C++ are such that it's
the exact opposite: C++ is significantly faster than C.

Just two simple examples are qsort() vs. std::sort() , and strlen()
vs std::string::length(). Especially in the first case it's precisely
the language features that help the compiler make a faster executable.
 
N

Nobody

I know I can develop faster by using C++, but the speed of program
in runtime is important too. So witch one is important? Can anyone
give a suggestion?

C code is usually either valid C++ code or is trivially modifiable to be
valid C++ code. Whether such code runs faster when compiled as C or as C++
depends upon the quality of the compiler, not the language.

However: C++ has additional features over C. Whether those features make
the program faster or slower depends upon how you use them.

No language feature is inherently "slow". Even if a particular feature has
a performance cost, if it simplifies development then the time saved can
be spent optimising other parts of the code.

The question isn't which language you would use if you wanted absolutely
optimal code (otherwise, everyone would use assembler). The question is
which language will produce the best result given the development time
available.

The answer to that question depends more upon the skill of the programmers
than on the language.

On one hand, C++ has features which will allow a good programmer to both
improve performance and reduce development time simultaneously. On the
other hand, not all programmers are good programmers, and C++ provides
not-so-good programmers with far greater scope for messing up.
 
B

BGB

Actually many equivalent constructs in C and C++ are such that it's
the exact opposite: C++ is significantly faster than C.

Just two simple examples are qsort() vs. std::sort() , and strlen()
vs std::string::length(). Especially in the first case it's precisely
the language features that help the compiler make a faster executable.

this again, comes down to coding practices...


also, just how many people really use "qsort()" anyways?...
IME, it is more often one of those largely forgotten functions (it is
there, but more often people write out their own sort logic manually).

(actually, it seems to be fairly common practice in C land for people to
largely ignore much of the standard library, and to write their own
logic for doing things).


it is also not clear that std::string would be, in general, faster, and
having an O(1) ability to fetch the length may turn out to be moot if
most other operations tend towards being more expensive. not that
worrying about micro-optimizing string operations is usually all that
relevant though anyways.


from what I can gather from checking online (via Google), it seems where
people have tried testing, the performance differences have tended to be
negligible.

some other tests have apparently shown std::string operations to be
about 4x slower than C-style strings.
 
I

Ian Collins

this again, comes down to coding practices...


also, just how many people really use "qsort()" anyways?...
IME, it is more often one of those largely forgotten functions (it is
there, but more often people write out their own sort logic manually).

(actually, it seems to be fairly common practice in C land for people to
largely ignore much of the standard library, and to write their own
logic for doing things).

That says it all really, C++ programmers don't have to.
 
J

Juha Nieminen

BGB said:
also, just how many people really use "qsort()" anyways?...
IME, it is more often one of those largely forgotten functions (it is
there, but more often people write out their own sort logic manually).

(actually, it seems to be fairly common practice in C land for people to
largely ignore much of the standard library, and to write their own
logic for doing things).

This is exactly one of the reasons why I dislike the language so much.
The C standard library does not offer many useful tools, and the reason
for this is that the language doesn't lend itself for this. It's very
hard to implement any generic data containers or algorithms which would
be efficient, and because they will always have some compromises,
inefficiencies or other defects, most C programmers end up not using
them anyways.

"Nobody uses qsort() anyways" is just the epitome of this. Contrast it
with std::sort(), which is actually a really decent general use sorting
function, and in the vast majority of cases it's pretty useful, and it's
very commonly used in C++ in most situations where sorting is needed.
Moreover, it's very easy to use (much easier than qsort()).
it is also not clear that std::string would be, in general, faster, and
having an O(1) ability to fetch the length may turn out to be moot if
most other operations tend towards being more expensive. not that
worrying about micro-optimizing string operations is usually all that
relevant though anyways.

Optimizing from O(n) to O(1) is far from "micro-optimizing". (And it's
not like you have to do anything in your code to achieve this.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top