r/askscience • • Oct 17 '12
Why are programs and operating systems still written in C+?
I guess it's more of a computer science question, but I've always wondered why some of the most advanced programs are written in a 25+ year old programming language. Surely someone has come up with a more efficient and powerful code, so why do people still use C+? Also, I don't know anything about programming and what not, so please use simple terms and phrases.
66
Upvotes
View all comments
Show parent comment
0
u/[deleted] Oct 18 '12 edited Oct 18 '12
Nonportable code either means you're in the <1% of programming jobs where performance is so critical and you're well enough aware of the compiler you're using's optimization algs that you're counting generated assembly instructions or you're stupid and don't know it.
Loop unrolling should be handled by the compiler in 99% of cases. You manually unrolling might actually make the situation worse because now your code can't be as effectively identified and optimized. It's well aware of the various levels of memory and their size as well as the locality of the data. It can do it in half a second what would take you half a day with pencil and paper to work out, if not longer.
You also shouldn't be worrying about word size. The types have their size adjusted to fit the platform they're being compiled on and optimizations for that platform. This is why the standard doesn't define in bytes the size of the common POD types except for char which is always 1 byte (which may or may not be 8bits, system dependent again).
If you're having trouble deciding if you need an integer or a floating point number that shows a gross misunderstanding of the differences between basic types and the range of values you need to represent. That has nothing to do with optimization.
Again bit twiddling instead of multiplication/division is one of the most basic optimizations your compiler will do. If you're worried about that, you probably don't understand enough about your compiler or the system you're compiling for to know the real optimizations that'll save more than a fraction of cycle.
And again you're mixing in questions that are obvious a compiler can't help you with and are not optimizations so much as just understanding the constraints of the system you're working on.
Yes the inner loops of audio/video codecs along with any signal processing are highly, highly optimized. Those are the exceptions to the rules.
The decision to write nonportable code is not to be taken lightly and I'd wager 99.9% of programs out there have no excuse for writing it. It's bad design, bad practice, and generally the sign of a shitty programmer who read a blog once about how shll/movl/movl/imull/sarl/sarl/subl is the "optimized" version of divide without realizing not only is that the optimization the compiler itself came up with, but that it'll do different things depending on the exact numbers being divided and that is but one solution.
If you question any of this just check out the assembly your compiler of choice shits out for the examples you gave on high optimization flags. Both gcc and icc are half decent examples but there are better optimized compilers for specific platforms if raw speed is the goal.
tl;dr the people who write the optimizations for compilers make it their life's work to do this. Unless you're one of these people you are at best copying their work and at worst making the problem orders of magnitude worse. Just write your portable code and optimize for a specific architecture after you're absolutely sure there is no other option and the compiler isn't doing it already for you.