High and Low Level Languages

Moderator
Posts: 1,140
Joined: 2005.07
Post: #16
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 Rasp) 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)
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #17
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.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #18
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..

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 623
Joined: 2007.09
Post: #19
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..

I'll buy that.Smile
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #20
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 this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #21
Continuing the Off-Topic:

There is a lot of C++ evangelization on that page!

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. Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #22
The comparison of std::sort and qsort is grossly unfair; it's perfectly possible to write a type-general sort in C that is equivalent in performance to std::sort, just that qsort isn't it.

Yes, C++ gives you some tools which can save time vs. C, but it also gives you extra complexity which is just as likely to chew that time back up again.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #23
OneSadCookie Wrote:C++ is both harder to program with and further from the hardware than C, where does that put it? Wink

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 Rasp
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  What languages do you need for a MMORPG? Lost22 12 6,022 Nov 20, 2007 01:07 AM
Last Post: Lost22
  Re-introduction of low level languages? Duane 4 3,278 Oct 30, 2007 03:10 AM
Last Post: OneSadCookie
  Pros and Cons of Different Languages Blacktiger 40 15,108 Aug 2, 2007 09:36 PM
Last Post: richard
  Some questions on languages from a new guy (...beginners) ProfessorApe 20 8,030 Jan 24, 2007 03:10 PM
Last Post: leRiCl
  Embedding Scripting Languages Tutorials Nick 18 7,543 Apr 13, 2006 09:53 PM
Last Post: kelvin