We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I've ported some code from C to C++ and I'm shocked at how much more slowly it runs. I've written a lot of C++ in the past, so I know I haven't done anything absurdly inefficient, but execution times have gone up by 150% (2.5 times slower!). Has anyone else seen anything like this? Are there any particular aspects of the Keil C++ implementation that I should look out for?
How about some sample code ? What exactly runs slower ?
a comment from the peanut gallery:
how can you choose ++ when concerned with performance?
Erik
C code compiled with a C++ compiler will compile into pretty much the same thing as with a C compiler.
If you "port" by rewriting the code, then you have to understand C++, and you have to understand the consequences of the features you ask for. If you start slinging around dynamic_casts, RTTI, and virtual functions, then you'll pay for them.
C++ has a "don't pay for what you don't use", and with modern compilers it works pretty well. The code is not going to be less efficient just because you call the file ".cpp".
For that matter, if you actually need a particular feature (say, dynamic dispatch at run time with virtual functions), the equivalent implementation in C (say, a table of function pointers or a bunch of switch statements) will typically cost you just as much after you get through coding it all by hand. Of course, if you compare apples and oranges (dynamic dispatch in C++ versus static compile-time linkage in C) you'll find that one feature is bigger or slower than the other. But that's not the language; that's the programmer.
The weakest link I know of used to be the standard I/O library. Many toolchains had trouble paring down the full couple hundred KB worth of I/O if you did just one "cout << 'string'". But that observation is pretty out of date. Major compilers probably do better with their libraries now.
So, the first step is to look at particular differences in your code and ask what it's doing that you don't expect that makes it so big and slow.
If you "port" by rewriting the code, then you have to understand C++, and you have to understand the consequences of the features you ask for
My post was with the thought in the background "why use ++ if you do not want to use the features"
There's too much code to post, but here's an overview.
The purpose of the function is to look for an address range (start address to end address) in a list of allocated address ranges. This involves doing a lot of address comparisons, where the addresses are 48 bits long. The C code uses a structure to hold the address which consists of an upper 16 bits and a lower 32 bits, and a function is called to compare structures.
The C++ implementation defines a class with the same member variables as the old C structure. Operator methods are the defined for the >= and <= operators, which perform the same comparison as the old C comparison functions. The operators use "pass by reference".
As the data and functions are fairly similar between C and C++, I wasn't expecting a huge difference in execution speed. I've had a quick look at the intermediate C produced by the C++ preprocessor, and it's virtually unreadable. I might have to give it more mental effort, but pointers in the right direction would be appreciated.
I'm working on some library code which is shared between several application. The plan is for the library to contain some generic implementations for general use, and each application can (if necessary) subclass a derivative which uses application specific optimisations.
Optimisations based on the application can be an order of magnitude (or more) faster than the generic implementations because they can eliminate unnecessary flexibility and they can bring additional resources to bear.
Can't you just keep it as a 'C' section in an otherwise C++ project?
Borland lets me do that...
Or build it separately as a Library?