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
Jul 29 '06
28 3104
Malcolm wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:4j******** *****@individua l.net...
>>Malcolm wrote:
>>>"ziman137" <ga********@yah oo.comwrote in message
1) Would the way I mix C and C++ code have any potential drawbacks to the performance?
Potentiall y yes. It must be compiled with a C++ compiler, and it is not impossible that the C++ compiler on a given system is inferior to the C compiler. However it is not very likely, often they share substantially the same code. If you use C++ constructs in your code you will find them harder to optimise than the C constructs, because C++ is designed to present a nice interface to the programmer rather than to expose basic processor operations. However you are probably aware of that already.
Eh? C++ can wrap basic processor operations in a higher level abstraction , but it can still expose then in the same way C does.
Can, yes. But Bjarne Strousup believes that malloc(), for example, should
not be used in new C++ code.
Correct, but you can provide your own version of new/delete, which puts
the developer back in the driver's seat.
Modern C++ using the standard library will have set of abstracted interfaces
to basic structures. They work well and might even be more efficient than
hand-coded similar C structures. However it is not possible for the
programmer to exert fine control over them. So for instance if he wants to
interate over an array, he will use the vector class iterator. This may be a
bare pointer, but probably it carries some run time checks with it to
protect the pointer from going out of bounds. Generally a good thing, but
harder to optimise.
He probably would, but if push came to shove, he could take the address
of the first element and use a plain pointer. Iterators tend to be well
optimised. Some implementations use raw pointers and none are required
to do any bounds checking, although this may be available as an
extension for testing.
C++ gives the developer a choice between the C way of doing something
and the C++ way.
--
Ian Collins.
Richard Heathfield <in*****@invali d.invalidwrites :
jacob navia said:
>There may be situations where C++ templates outperform assembly language,
Jacob's talking through his hat again. His claim is easily disproved. All we
have to do is tell the compiler to generate assembly language from the C++
template code. We now have assembly language that performs exactly as well
as the C++ template code. And, given an experienced assembly language
programmer (to match the experienced C++ programmer who produces such
astoundingly quick code), we can almost certainly find an optimisation,
however trivial, that will make the assembly language version at least a
little faster than the C++ template version from which it was generated.
On the other hand, for any language at a higher level than assembler,
the compiler may find opportunities for optimizations that someone
creating hand-written assembler wouldn't find, or would choose not to
use.
For example, if I'm writing low-level code that I expect to be
maintained, I'm not likely to perform a pervasive optimization that
works only if an array size is a power of 2.
A skilled assembly language programmer is likely to be much smarter
than an optimizing compiler, but an optimizer has a different set of
tradeoffs to consider. A programmer will perform optimizations over
time as he's working on a piece of code; an optimizing compiler will
redo all its work from scratch every time it's invoked.
Yes, you can use an optimizing compiler as a way to generate assembly
language -- but that argument doesn't apply to hand-written assembly
language.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson said:
<snip>
>
On the other hand, for any language at a higher level than assembler,
the compiler may find opportunities for optimizations that someone
creating hand-written assembler wouldn't find, or would choose not to
use.
Sure, but perhaps you missed my point, which is that the /starting point/
for the assembly language programmer would be the super-efficient C++
template code, expressed in assembly language form. Given that starting
base, (a) it's bound to be as fast as the C++ template code, because it's
the same code that the C++ template code is compiled to - which is in
itself sufficient to destroy Mr Navia's point, and (b) it would be quite
surprising if the skilled assembly language programmer couldn't find /one/
hand-optimisation to make the asm form just a tiny smidgenette faster.
<snip>
Yes, you can use an optimizing compiler as a way to generate assembly
language -- but that argument doesn't apply to hand-written assembly
language.
It does if the starting point for the handwriting is the generated code.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Richard Heathfield wrote:
Keith Thompson said:
<snip>
>>On the other hand, for any language at a higher level than assembler, the compiler may find opportunities for optimizations that someone creating hand-written assembler wouldn't find, or would choose not to use.
Sure, but perhaps you missed my point, which is that the /starting point/
for the assembly language programmer would be the super-efficient C++
template code, expressed in assembly language form. Given that starting
base, (a) it's bound to be as fast as the C++ template code, because it's
the same code that the C++ template code is compiled to - which is in
itself sufficient to destroy Mr Navia's point, and (b) it would be quite
surprising if the skilled assembly language programmer couldn't find /one/
hand-optimisation to make the asm form just a tiny smidgenette faster.
I don't know if you do this, but I often find I can obtain more
productive optimisations by using the generated assembly language to
tune the high level language code rather than hand editing the assembly.
One area optimising compilers have an edge and will continue to improve
is global optimisations, there is only so much code a human can retain
in working memory. We can often do better with micro-optimisations, but
the compiler is better equipped to see the big picture.
--
Ian Collins.
Richard Heathfield <in*****@invali d.invalidwrites :
Keith Thompson said:
<snip>
>> On the other hand, for any language at a higher level than assembler, the compiler may find opportunities for optimizations that someone creating hand-written assembler wouldn't find, or would choose not to use.
Sure, but perhaps you missed my point, which is that the /starting point/
for the assembly language programmer would be the super-efficient C++
template code, expressed in assembly language form. Given that starting
base, (a) it's bound to be as fast as the C++ template code, because it's
the same code that the C++ template code is compiled to - which is in
itself sufficient to destroy Mr Navia's point, and (b) it would be quite
surprising if the skilled assembly language programmer couldn't find /one/
hand-optimisation to make the asm form just a tiny smidgenette faster.
<snip>
>Yes, you can use an optimizing compiler as a way to generate assembly language -- but that argument doesn't apply to hand-written assembly language.
It does if the starting point for the handwriting is the generated code.
If the optimizing compiler generates code that pervasively depends on
some semantically trivial aspect of the high-level source, the
generated assembly won't be a good basis for maintenance. For
example, a compiler might generate radically different code if an
array size is a power of two than if it isn't, even if there's only,
say, a 1% advantage. If you change the array declaration, the
compiler will happily re-write the entire application when you
recompile it. A sane assembly language programmer would probably
avoid such an optimization if it would hurt maintainability that much.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Richard Heathfield wrote:
Keith Thompson said:
<snip>
>On the other hand, for any language at a higher level than assembler, the compiler may find opportunities for optimizations that someone creating hand-written assembler wouldn't find, or would choose not to use.
Sure, but perhaps you missed my point, which is that the /starting point/
for the assembly language programmer would be the super-efficient C++
template code, expressed in assembly language form. Given that starting
base, (a) it's bound to be as fast as the C++ template code, because it's
the same code that the C++ template code is compiled to - which is in
itself sufficient to destroy Mr Navia's point, and (b) it would be quite
surprising if the skilled assembly language programmer couldn't find /one/
hand-optimisation to make the asm form just a tiny smidgenette faster.
<snip>
>Yes, you can use an optimizing compiler as a way to generate assembly language -- but that argument doesn't apply to hand-written assembly language.
It does if the starting point for the handwriting is the generated code.
The real point is that the compiler-generated assembly code ain't
necessarily a good starting point, for it perhaps takes advantages of
accidental coincidences of data that a human programmer will not indulge
in. So (maintainable) assembly code _may_ be worse than
compiler-generated, the point Keith Thompson had made pretty clear.
Example:
The line
int x = NUMBER;
is compiled (for ARM) differently depending on
#define NUMBER 1
#define NUMBER 257
#define NUMBER 0x101010101
A human would stick to a single implementation.
Ian Collins wrote:
Malcolm wrote:
>"Ian Collins" <ia******@hotma il.comwrote in message news:4j******* ******@individu al.net...
>>Malcolm wrote:
"ziman137" <ga********@yah oo.comwrote in message
1) Would the way I mix C and C++ code have any potential drawbacks to the performance? > Potentiall y yes. It must be compiled with a C++ compiler, and it is not impossible that the C++ compiler on a given system is inferior to the C compiler. However it is not very likely, often they share substantially the same code. If you use C++ constructs in your code you will find them harder to optimise than the C constructs, because C++ is designed to present a nice interface to the programmer rather than to expose basic processor operations. However you are probably aware of that already. Eh? C++ can wrap basic processor operations in a higher level abstraction , but it can still expose then in the same way C does.
Can, yes. But Bjarne Strousup believes that malloc(), for example, should not be used in new C++ code.
Correct, but you can provide your own version of new/delete, which puts
the developer back in the driver's seat.
>Modern C++ using the standard library will have set of abstracted interfaces to basic structures. They work well and might even be more efficient than hand-coded similar C structures. However it is not possible for the programmer to exert fine control over them. So for instance if he wants to interate over an array, he will use the vector class iterator. This may be a bare pointer, but probably it carries some run time checks with it to protect the pointer from going out of bounds. Generally a good thing, but harder to optimise.
He probably would, but if push came to shove, he could take the address
of the first element and use a plain pointer. Iterators tend to be well
optimised. Some implementations use raw pointers and none are required
to do any bounds checking, although this may be available as an
extension for testing.
C++ gives the developer a choice between the C way of doing something
and the C++ way.
Ehmmm... please help me here: I am not a C++ guy.
AFAIK, C++ does not allow all the C way (example: tentative declarations).
Then, if I write C-style in C++, at the very minimum the C++ compiler
doesn't know e.g. about any exceptions that may be thrown in a function
I call and which is in a different translation unit. So it just has to
bring in the heavy machinery in just in case. Am I missing something?
Ark wrote:
Ian Collins wrote:
>C++ gives the developer a choice between the C way of doing something and the C++ way.
Ehmmm... please help me here: I am not a C++ guy.
AFAIK, C++ does not allow all the C way (example: tentative declarations).
Then, if I write C-style in C++, at the very minimum the C++ compiler
doesn't know e.g. about any exceptions that may be thrown in a function
I call and which is in a different translation unit. So it just has to
bring in the heavy machinery in just in case. Am I missing something?
If my understanding of tentative declarations is correct, they are
synonymous with forward declarations in C++ due to the differing use of
'struct' in a declaration, that is:
struct X;
forward declares the type X.
The compiler doesn't have to bother with unexpected exceptions when
compiling idiomatic C code, any unhandled exception can be caught by
whatever calls main().
As a trivial example, on my platform the following generated essentially
the same code when compiled as C or C++:
extern int fn(void);
extern void fn1(int);
typedef struct { int n; } X;
int main(void)
{
X x;
x.n = fn();
fn1( x.n );
}
--
Ian Collins.
Ian Collins wrote:
Ark wrote:
>Ian Collins wrote:
>>C++ gives the developer a choice between the C way of doing something and the C++ way.
Ehmmm... please help me here: I am not a C++ guy. AFAIK, C++ does not allow all the C way (example: tentative declarations). Then, if I write C-style in C++, at the very minimum the C++ compiler doesn't know e.g. about any exceptions that may be thrown in a function I call and which is in a different translation unit. So it just has to bring in the heavy machinery in just in case. Am I missing something?
If my understanding of tentative declarations is correct, they are
synonymous with forward declarations in C++ due to the differing use of
'struct' in a declaration, that is:
struct X;
forward declares the type X.
The compiler doesn't have to bother with unexpected exceptions when
compiling idiomatic C code, any unhandled exception can be caught by
whatever calls main().
As a trivial example, on my platform the following generated essentially
the same code when compiled as C or C++:
extern int fn(void);
extern void fn1(int);
typedef struct { int n; } X;
int main(void)
{
X x;
x.n = fn();
fn1( x.n );
}
1. Tentative declarations of variables are (in C) indistinguishab le from
definitions until the end of the translation unit; they AFAIK expressly
prohibited in C++. E.g., a (const) circular data structure like
typedef struct X {int x, const struct X *next} X;
const X x;
const X y;
const X z = {5, &x};
const X y = {6, &z};
const X x = {7, &y};
is a valid C but not C++.
2. Interesting example; just curious what the differences are and
whether it matters that your function is called main, not, say, foo.
Ark wrote:
Ian Collins wrote:
>Ark wrote:
>>Ian Collins wrote:
C++ gives the developer a choice between the C way of doing something and the C++ way.
Ehmmm... please help me here: I am not a C++ guy. AFAIK, C++ does not allow all the C way (example: tentative declarations) . Then, if I write C-style in C++, at the very minimum the C++ compiler doesn't know e.g. about any exceptions that may be thrown in a function I call and which is in a different translation unit. So it just has to bring in the heavy machinery in just in case. Am I missing something?
If my understanding of tentative declarations is correct, they are synonymous with forward declarations in C++ due to the differing use of 'struct' in a declaration, that is:
struct X;
forward declares the type X.
The compiler doesn't have to bother with unexpected exceptions when compiling idiomatic C code, any unhandled exception can be caught by whatever calls main().
As a trivial example, on my platform the following generated essentially the same code when compiled as C or C++:
extern int fn(void); extern void fn1(int);
typedef struct { int n; } X;
int main(void) { X x;
x.n = fn();
fn1( x.n ); }
1. Tentative declarations of variables are (in C) indistinguishab le from
definitions until the end of the translation unit; they AFAIK expressly
prohibited in C++. E.g., a (const) circular data structure like
typedef struct X {int x, const struct X *next} X;
typedef struct X {int x; const struct X *next;} X;
Is valid C but not C++.
typedef struct X {int x; const X *next;} X;
Is valid C++ but not C.
typedef struct X_t {int x; const struct X_t *next;} X;
Is valid C++ and C.
const X x;
const X y;
const X z = {5, &x};
const X y = {6, &z};
const X x = {7, &y};
is a valid C but not C++.
Correct , C++ prohibits uninitialised consts. Easily fixed for both thus:
extern const X x;
extern const X y;
const X z = {5, &x};
const X y = {6, &z};
const X x = {7, &y};
2. Interesting example; just curious what the differences are and
whether it matters that your function is called main, not, say, foo.
The C++ compiler pushed one extra register on the stack. The function
name does not make any difference.
--
Ian Collins. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: bergel |
last post by:
Hello,
Does anyone already have some experience in mixing AWT and Swing? Is it
conceptually doable?
Does the design of Swing prevent interaction between an AWT and a Swing
widget?
Regards,
Alexandre
|
by: Russell E. Owen |
last post by:
At one time, mixing for x in file and readline was dangerous. For
example:
for line in file:
# read some lines from a file, then break
nextline = readline() # bad
would not do what a naive user might expect because the file iterator
buffered data and readline did not read from that buffer. Hence the call
to readline might unexpectedly skip some lines.
|
by: Erik Max Francis |
last post by:
Is there any prohibition against mixing different protocols within the
same pickle? I don't see anything about this in the Python Library
Reference and, after all, the pickle.dump function takes a protocol
argument for each time it's called. (This is in Python 2.3.3.)
I have a pickle containing two objects: a tag string and a (large)
object containing many children. The identifying string is there so
that you can unpickle it and...
|
by: Rudolf |
last post by:
Is it possible to add a vb.net source code module to a c#
project and if so how?
Thanks
Rudolf
|
by: Marc Cromme |
last post by:
I would like to ask a question about (good ?) style and possibilities in
mixing C FILE* and C++ file streams.
The background is that I want to use the C libpng library from within C++,
but I would like to open C++ file streams due to easier exception
handeling and safe closure of file ressources.
Question 1:
I open a standard file stream and want to transfer some binary read bits
| |
by: Cristian Tota |
last post by:
Hi,
I'd appreciate any thoughts on mixing C++ and C code. I have a project that
uses a given C interface, the rest of the project can be either in C or C++.
What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone...
|
by: Neil Zanella |
last post by:
Hello,
Often I happen to be dealing with nonnegative integers and since I know I won't
need negative numbers here I declare them as unsigned simply to make the program
somewhat clearer. Effectively, though, signed integers would often work too since
unless I am doing some modular arithmetic modulo the word length then I almost
never need to use the high bit since the integers I deal with are usually not
that large, and I would assume...
|
by: Dan |
last post by:
Hi
What are the dangers of mixing asp and asp.net? For the .net part of the
site i will need to use the global.asax file but for the asp parts it will
be using the other global. file, is there anyway to get to both use the same
one?
For example i want to convert my homepage to aspx first, but in doing so my
session var that needs to be initialised wont be as it will use the
global.asax and not the global.asa.
|
by: ziman137 |
last post by:
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.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |