High and Low Level Languages

A lot of people have a rather liberal view of a low level language. The more abstraction over the hardware (real or virtual), the higher level the language. You can stretch and call C a low level language, since it follows assembly a lot (in terms of ability and features) except for being portable and allowing compound statements. (for example: a math statement with parenthesis and several operators being used) C++ on the other hand, would be more of a high level language with several low level features, such as the ability to more or less write C code and access to all the lower level aspects, such as pointers.

OSC always talks about how C is always better than C++ in every circumstance. I think I will call him out this time. C++ may be rather complicated (templates are the main offender, though they can be very useful), but it's still much easier to make an inheritance tree with C++ than straight C, especially once you add virtual functions. A couple of times I've thought about how cool it would be to implement that kind of thing using straight C, but it's going to be painful and awkward. For example, as a home project, I'm creating a ray tracer. It's simple in C++ to have a high level interface for various things such as "Object" and "Material", but it would be a pain to implement it in C. Also, templates can simplify things greatly despite being complicated to work with themselves. (they are complicated to create, not so much to use ) For example, if you want to have a member function callback, possibly even with an arbitrary, type-safe parameter. It would be even more complicated to do this completely in C, maybe even impossible to make it totally type safe. (you might be able to do it with macros, though)

Point to take home: the higher level the language, the easier it can be. There are exceptions of course, such as prolog, but that's because of the programming style rather than the language itself. However, the lower the level, the faster it can be. One major exception to that rule, however, is templated C++ code can be faster than C code due to the way templates work. The reason why I like C++ myself is it's higher level than C, providing abstractions for objects etc., but still has enough low level aspects so you can think about what's going on under the hood. (a usual argument against that are references, templates, and operator overloading complicating things, but they are essentially pointers, code duplication, and function calls (for non-primitives) respectively)

akb825 Wrote:One major exception to that rule, however, is templated C++ code can be faster than C code due to the way templates work.

My first reaction was "uh... I guess that might be true if you don't know how to program with C...", but perhaps there is something I don't know about templates. How can using templates make the code faster if it compiles down to roughly the same ASM, except for structural differences? Is there some way to not have as many stack frame creations or something mysterious going on under the hood with C++ that you can't do with C? Care to elaborate? (I believe you on face value, because you're a pretty sharp guy, but I've never heard that before so I'm in the dark on this)

To me, C++ is every bit as low-level as C because the end result is ASM after compilation. But as far as syntax goes it is clearly higher level. It seems to me that it is often too difficult to combine those two dimensions (syntax plus abstraction level) accurately when comparing any language against another comparable one in a useful way, which is why I stick to the single dimension of hardware abstraction for hi v lo. I can't see C++ as being "farther from the hardware" than C. Pre-compilation, yes. Post-compilation no.

Hairball183 Wrote:What's the difference? Are High level languages more powerful than low level languages? Are they faster? It came up as I was working through a C tutorial and it mentioned that C was middle-level, and Java and C++ were High level.

It's not a worthwhile distinction, languages are languages.. they compute things.. Knowing if one is "high" or "low" level is just something to argue endlessly about, and water down the real meaning of concepts like compiler etc..

unknown Wrote:It's not a worthwhile distinction, languages are languages.. they compute things.. Knowing if one is "high" or "low" level is just something to argue endlessly about, and water down the real meaning of concepts like compiler etc..

AnotherJake Wrote:My first reaction was "uh... I guess that might be true if you don't know how to program with C...", but perhaps there is something I don't know about templates. How can using templates make the code faster if it compiles down to roughly the same ASM, except for structural differences? Is there some way to not have as many stack frame creations or something mysterious going on under the hood with C++ that you can't do with C? Care to elaborate? (I believe you on face value, because you're a pretty sharp guy, but I've never heard that before so I'm in the dark on this)

C++ templates can be faster than C because it's equivalent to completely re-writing your code specifically for that type. The classic example of this is the sort() function, which is faster in C++ than the qsort() function in C. This is because in C, you must have function pointers for the compare functions. In C++, on the other hand, you can have the compare function (defaulting to a < compare) passed in as a template parameter. If that function is set to be inlined, then it can be, removing that overhead. After a quick google search, this page from Stanford addresses this.

Quote:STL's solution exceeds the best solutions (special-case library functions or my hand-written code) in C, in terms of execution speed.

Ugh... yet another comparison of C++ versus *my* hand-written C code! If I had a nickel for every time I... never mind. I'm not trying to split hairs here, but I see where this is going and I think I'd better leave it alone.

OneSadCookie Wrote:C++ is both harder to program with and further from the hardware than C, where does that put it?

Well, that depends. If you want to use every single language construct C++ offers, yes, it is harder to program and further from the hardware *but* you can also use it as an augmented C: taking advantage of templates, classes with inline methods and operator overloading, and be just as close to the hardware.

edit: posted before reading page 2 of this thread... it seems my points have already been discussed