ziman137 wrote:
> 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
Hello Gary,
I've recently gone through the process of deciding to use C or C++ for
a high performace "algorithmic" library.
As far as raw performance goes, you'll find benchmarks throughout the
net going both ways. Since C++ is more popular, you'll see more claming
C++ is as fast/faster. At the end of the day it seems that if you just
want to generically compare performance of C vs C++ you'll arrive at
the answer "it depends on how well you know and understand the
language".
I'm much more familiar with C. I know how it works, I know how to make
nice API's and modular code with it and I know how to avoid some issues
that might degrade performance. But these will apply to both C++ and C
for the most part since C++ is a superset, things like use narrow
ranged case statements, avoid deeply nested if statements, using the
smallest integer types isnt always beneficial etc etc.
However, I don't know much about how templates are internally handled,
I don't know how multiple inheritance works and I have only a small
notion of how virtual functions work. So the problem for me is that I
don't know how to write efficient C++ code because I don't fully
understand how it all works. Not only that but because I've used C
since forever, I don't have much intuition when it comes to OO
application design. So I stick with C. Call me lazy
So my advice, either stick with what you know or learn the performance
pitfalls.
Cheers,
Chris
P.S. as for combining C++ and C in a scientific libray, check out
openCV. It's a computer vision API written like C but with both a C and
C++ interface.