C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A concern about mixing C and C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
ziman137
Guest





PostPosted: Sat Jul 29, 2006 8:02 pm    Post subject: A concern about mixing C and C++ Reply with quote



Hello all,

I have a question and am seeking for some advice.

I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++. However, I thought it might be
convenient to use some C++ code at some misc places. I'm aware that, I
could always use the C++ compiler to get it work.

My concerns are:

1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?

My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).

Thanks,

Gary


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
R.F. Pels
Guest





PostPosted: Sat Jul 29, 2006 9:26 pm    Post subject: Re: A concern about mixing C and C++ Reply with quote



ziman137 wrote:

Quote:
I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++. However, I thought it might be
convenient to use some C++ code at some misc places. I'm aware that, I
could always use the C++ compiler to get it work.

Mind you, this first part is (partly) tongue-in-cheek. You're violating at
least three rules here:

- Build on the work of others.
- Thou shalt not optimise.
- Thou shalt not optimise... yet.

More serious now.

On 'building on the work of others', I think these kind of libraries are
quite abundant, and I cannot see why you are not using one of them or why
you are not extending one of them. Do a bit of research into this area, and
you might come to the conclusion that the need to write such a thing does
not exist.

On 'optimisation' I would like to say that concerns about optimal code
should be warranted by measurement, not by your gut feeling.

Besides, optimal code is not the only parameter that influences the
usability of an algorithmic library. Consistency of interface, ease of use,
robustness, maintainability and extensibility are also important, even if
you do not envision sharing your library in the first place. And from that
point of view I think that C++ is the better choice.

--
ruurd

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alex
Guest





PostPosted: Sun Jul 30, 2006 1:43 am    Post subject: Re: A concern about mixing C and C++ Reply with quote



ziman137 wrote:

Quote:
1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?

Gary,

The answer to both your questions is: it depends. There is nothing in
the language(s) that would cause the answer to be yes (C++ is a
superset of C), but you can certainly create drawbacks yourself if you
are not careful.

Quote:
My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).

It's hard to give a quick and all encompassing advice on this, but even
with performance concern, you can still use OO. Steer clear of virtual
functions (use generic approach instead if you need polymorphism) and
take advantage of inlining whenever appropriate and reasonable.

Alex


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Sun Jul 30, 2006 1:44 am    Post subject: Re: A concern about mixing C and C++ Reply with quote

ziman137 wrote:

Quote:
I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++.

What makes you believe that C++ is slower than C? Programs are slow
because algorithms are badly choosen, not because a particular language
is "slow". With proper care, C++ programs can be made very fast, and it
is likely easier to write fast algorithms in C++ rather than in C.

Quote:
However, I thought it might be
convenient to use some C++ code at some misc places. I'm aware that, I
could always use the C++ compiler to get it work.

My concerns are:

1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?

This is nothing that could be guaranteed by any standard, but for the
compilers I'm aware of, there's no additional code complexity involved
when mixing C and C++.

Quote:
2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?

Yes. It makes it harder to read and to understand, and thus harder to
maintain.

Quote:
My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).

For maximum performance, I would rather suggest to use C++. Especially
templates are a very worthful tool to specialize algorithms for specific
cases where you would just duplicate code in C.

Compare for example the way how qsort() of the C standard library has to
work compared to a C++ version: The C version requires a callback and
thus has call-overhead, a templated C++ version would be not only easier
to read, but it would also be able to inline the operator< of the class
type to be sorted for the comparison. You could also specialize the sort
algorithm to, say, integer types for choosing a sort-algorithm that is
more suitable, i.e. a radix sort, for this special data type.

So long,
Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sun Jul 30, 2006 5:01 am    Post subject: Re: A concern about mixing C and C++ Reply with quote

In article <1154150846.989253.46370 (AT) m73g2000cwd (DOT) googlegroups.com>,
ziman137 <gangxu_csu (AT) yahoo (DOT) com> writes
Quote:
My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).

You have of course measured the performance of the most efficient C code
and the most efficient C++ code? Several world class experts have been
surprised when they did that in some cases.

If you are writing for C++ use, write C++ but of course that includes
using procedural code from time to time. Even code that behaves
identically when compiled as C.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Rob
Guest





PostPosted: Sun Jul 30, 2006 6:27 am    Post subject: Re: A concern about mixing C and C++ Reply with quote

ziman137 wrote:

Quote:
Hello all,

I have a question and am seeking for some advice.

I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++. However, I thought it might
be convenient to use some C++ code at some misc places. I'm aware
that, I could always use the C++ compiler to get it work.

My concerns are:

1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?

My intention for choosing C interface instead of C++ OOD is to gain
the maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).


You are worrying about optimising performance, but you haven't actually
specified the purpose of your library or who will be using it.

C does not inherently give more performance than C++: it just provides
a different set of mechanisms and each of those have trade-offs in terms
of performance, memory footprint, usability, etc etc. C++ is (almost)
a superset of C89, but C99 provides some reasonably significant features
that are not part of C++. C allows you to do things at a "lower level"
(eg less abstraction) than C++ which can give good performance if good
algorithms and implementation techniques are employed, and can perform
very poorly of poor algorithms and implementation techniques are
employed. That statement, while very obvious, is something you're
overlooking.

By using C++ features, you get the benefit of the fact that good quality
compilers will probably do things more efficiently than what you could
achieve if you "rolled your own" equivalent in C. A potential drawback
of that is that you're at the mercy of quality of implementation of the
compiler. By using C features, you can benefit by having more control
over the nuts and bolts of implementing the algorithm. The drawback is
that it requires more effort and skills to get those nuts and bolts
right, and humans are don't tend to be that good at optimising code
for a computer: things that humans focus on when optimising code are
often not the things that really affect the machine.

Another drawback of using a C approach is that, if the library is to be
used from C++, that it will have an interface that may be a thing of
beauty to a C programmer, but could be very clunky for a C++ programmer.
For example, a C++ approach might involve objects that carry necessary
information about the data (eg dimensions of an array) with them but,
in C, it may be necessary to pass several arguments to an algorithm
to represent the same thing (eg a pointer to data, the dimension of the
array, etc). A corresponding drawback of the C++ approach, OTOH, is
that interface styles that are good practice in C++ are ofen not usable
from C and that the interface might hide very expensive operations (eg
copying large data structures behind the scenes) if the algorithm is not
well implemented.

And when it comes to performance, the only way to be sure which approach
(vanilla C or vanilla C++) is more efficient is to test it. There is
certainly no inherent advantage that C has over C++, or C++ has over C,
in terms of performance. There are features of both languages that
can be used to advantage or misused greatly. The core workings
of the algorithm itself are usually more significant than a choice
between C and C++.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
galathaea
Guest





PostPosted: Sun Jul 30, 2006 8:10 pm    Post subject: Re: A concern about mixing C and C++ Reply with quote

ziman137 wrote:
Quote:
Hello all,

I have a question and am seeking for some advice.

I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++. However, I thought it might be
convenient to use some C++ code at some misc places. I'm aware that, I
could always use the C++ compiler to get it work.

My concerns are:

1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?

My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).



to add to the good advice others have given...


there are many cases where the performance of c++ code
can outperform c code

this is because the compiler can be presented more static knowledge
on the copyability and constness of data sections in c++

check out
for instance
expression templates

these techniques are how some of the best algorithmic libraries
have been constructed
(even outperforming fortran)

the problem in the past
that has given fuel to this impression
that c++ is slower than c

is a mistaken belief that all c++ code
must use inheritance
virtual dispatch
and other costly runtime mechanisms
(and also that the c++ standard libraries
have traditionally wrapped the c standard libraries
and therefore have regularly been slower)

use static mechanisms where static choices are made
stay const correct
and consider collecting static information about complex operations
in expression templates when profiling points to excessive copies

those type of criteria point to using c++


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
galathaea: prankster, fablist, magician, liar


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dave Steffen
Guest





PostPosted: Tue Aug 01, 2006 5:38 am    Post subject: Re: A concern about mixing C and C++ Reply with quote

"ziman137" <gangxu_csu (AT) yahoo (DOT) com> writes:

Quote:
Hello all,

I have a question and am seeking for some advice.

I am currently working to implement an algorithmic library. Because
the performance is the most important factor in later applications, I
decide to write it in C instead of C++. However, I thought it might be
convenient to use some C++ code at some misc places. I'm aware that, I
could always use the C++ compiler to get it work.

My concerns are:

1) Would the way I mix C and C++ code have any potential drawbacks to
the performance?
2) Would the way I mix C and C++ code have any potential drawbacks for
the future users to use the library?

My intention for choosing C interface instead of C++ OOD is to gain the
maximum performance as possible, yet I still like to use some C++
coding features (e.g., "const", reference instead of pointers, ...).

FWIW I just spent a little time profiling several open source
vector/matrix libraries with a good profiler (valgrind / callgrind +
kcachegrind, very nice).

The only straight C library I looked at was GNUSSL. I looked at a
bunch of C++ libraries, including TVMET, uBLAS, and a couple of
home-rolled varieties.

GNUSSL was on par, but not necessarily faster than, a fairly
straightforward C++ implementation. It was a much faster than uBLAS
(but that's not fair, I didn't spent a lot of time figuring out how
to make uBLAS fast, and suspect I wasn't using it well). It
couldn't touch TVMet, or a home-rolled static (e.g. compile-time
sized) implementation.

The moral of the story is that

A) anything you can do in C, you can do in C++
B) "you can write slow code in any language" (quoted, I think, from
Scott Meyers)
C) If you know what you're doing, C++ can be significantly faster
than C.

As others have said here, you're _probably_ starting at the wrong end.
Design your library and its interface first, using good design
principles. Then write it, and loads o' unit tests, and make it
work correctly. Then, and _only_ then, do you pull out a profiler
and make it fast.

----------------------------------------------------------------------
Dave Steffen, Ph.D.
Software Engineer IV Disobey this command!
Numerica Corporation
ph (970) 419-8343 x27
fax (970) 223-6797 - Douglas Hofstadter
dgsteffen at numerica dot us

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.