Mailinglist Archive: opensuse (2773 mails)
| < Previous | Next > |
Re: [SLE] Choosing a programming language ( Offtopic/maybe )
- From: activex1@xxxxxxx (Sam Carleton)
- Date: Wed, 22 Dec 1999 15:58:41 -0500
- Message-id: <38613B81.26F0D50C@xxxxxxx>
Stuart Hall wrote:
> I am in the same boat as you, however I have made my choice, and I am
> sticking with it, damnit! :-)
>
> I chose C++ - mostly because I can then take that knowledge and port
> it pretty easily to Java.
Additional thought on why I recommend C over C++. Because C++ is object
oriented, there are objects that are simply wrappers around the C code.
There are many times when even though I am coding in C++, I take the C
approach to a few lines of code simply because using an object adds a
great amount of overhead. Here is a prime example:
In C++ using the std::string class, someone might do this:
td::string str;
...
str = "This " + "is a " + "test";
This is fine, legal, and it works great! But if you use a debugger, you
discover that for every literal that you have, a temparary std::String is
created. Everytime a std::string is created this way, memory is
allocated. For those that don't know, allocating memory is expensive, it
takes a lot of time to process the allocation. With each creation of
std::string, there are at least two function calls, the contructor,
destructor. In most if not all the temparary std::strings, there is at
least one more function called that does an assignment. Inside the object
it's self it might call a number of functions to do everything it needs to
do. Depending on how the code is compiled these all might turn out to be
inline functions, but they might all remain as function calls. A function
call in and of itself is not very expensive, but if each temparary
std::string results in six functions calls, the cost begins to mount. To
top it all off, for every literal + literal, there is another temparary
std::string created. By the time that one line of code has completed
executing, there are SIX temparary std::string created! With all the
memory being allocated and deallocated and maybe as many as 24, if not
more function calls, this code is VERY inefficient.
I have seen code like that above from those that know C++ but never
learned C.
In C the equivallent is this:
char str[1024]; // or any size that you know
// is going to be enough, you
// might do a memory allocation
// here instead to set the size
// dynamically, or to get the memory
// from the heap rather then the stack
trcpy( str, "This ");
strcat( str, "is a");
strcat( str, "test");
These functions are simple loops that copy one string to another. Three
function calls and no memory allocations. You can only improve appon this
if there is a custon function writen to copy three strings into a fourth.
I do not believe there is such a function in the standard C library.
This is only one examply of how it is very easy to write VERY inefficient
code with C++ if you don't have a good understanding of C. I don't think
there is anyone in this mailing list that would disagree with me that the
attitude of "if it runs too slow through more hardware at it" is a bad
attitude.
If you understand what the language is doing for you, or at times to you,
it is not very hard at all and quickly becomes second nature to do
something in a different way making it far more efficient.
Sam
--
To unsubscribe send e-mail to suse-linux-e-unsubscribe@xxxxxxxx
For additional commands send e-mail to suse-linux-e-help@xxxxxxxx
Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
| < Previous | Next > |