Date: Sun, 16 Apr 2006 03:33:35 -0700 (PDT) From: Dan Strick <strick@covad.net> To: freebsd-chat@freebsd.org Subject: Re: Why is not more FreeBSD software written in C++? Message-ID: <200604161033.k3GAXZSK001785@mist.nodomain>
next in thread | raw e-mail | index | archive | help
OOPS ... I accidentally sent off that last message on the subject: Re: Why is not more FreeBSD software written in C++? while trying to run ispell on it. In addition to fixing a bunch of typos that I trust everyone will just ignore, I wanted to add: I really like C++ but I am not sure how to deal with the performance problems. They are not so trivial that they can always be ignored. I have done a few casual benchmarks. Code that uses C++ strings can run up to 100 times slower than functionally equivalent code that uses C character arrays (though such an extreme case is surely pathological). Code that uses C++ iostreams typically runs somewhere between 2 and 9 times slower than code that uses C stdio. You don't believe me? Compare cout << ' '; to putchar(' '); and weep. Of course most programs presumably spend only a small part of their runtime on strings and i/o. I wish I could say the the advantages of C++ are so great as to outweigh any possible performance glitch, but I can't quite bring myself to do that. Dan Strick --------------------------------------------------------------------- P.S. In case you missed the message to which this one applies, here it is: Most software is probably written in C rather than C++ because fewer programmers are familiar with C++. Note that this means C++ programs are in a practical sense less portable and more difficult to maintain even though the GNU C++ compiler is a good implementation, widely available and pretty much standard in all unix-like OS distributions. It may be just as well that most software is written in C. Programs written in C probably run only a very few percent slower when compiled as C++. (I am assuming the programs are written in the common subset language.) Even language features peculiar to C++ are generally implemented quite efficiently. Programs written in a good C++ style naturally use C++ standard library facilities (classes, private functions, templates) that can be expensive. Since C++ programmers generally do not consider the underlying implementations (arguably a very good thing), significant unintended run-time overhead can result. For example, I once rewrote a program in C++ and discovered the C++ version ran about 5.5 times slower than the original C version. My "mistake": I used the C++ string and iostream classes. After rewriting the C++ program to use C stdio instead of C++ iostreams and replacing C++ string classes with C character arrays, the C++ program ran only 1.5 times slower than the C program. I must note that the C++ version was in some ways a lot cleaner than the C version. The problem is that a major reason for using C++ in the first place is to take advantage of these specific C++ library features. A major motivation for the development of C++ itself was to facilitate code sharing by better isolating main program code from library implementation details. A C++ program that avoids using simplifying standard library facilities by reimplementing them is arguably bad C++.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200604161033.k3GAXZSK001785>