Wednesday, May 05, 2004

So what's really cool about C++, you ask ? After all, all those extra features must really slow it down, right ? Or maybe it makes code harder to read ? Perhaps it's harder to track meory leaks ? (in comparison to C or Java)


It's ALL About Good Coding Practices (Overused & trite, but true)
It's all in how you code.

For instance,

(unoptimized)C code is WYSWYG (What you see is what you get), where you can access any part of memory and change much of it, etc. It's also dangerous because you can easily access parts of memory you didn't intend to, due to a mistake.

C++ code can literally do everything that C can ... although some implicit side effects must be explicitly labelled in C++ rather than C. C++ code can also directly link to C code via a "preprocessor" statement called extern "C" {/*This is where C code or links to C code would be*/}
C++ has more features, and a few of those features can slow down the program if you don't know what you're doing.
The major features are:

1.) default function parameters (you can instruct a function to take certain extra "optional" inputs )

Speed: No slowdown.

2.) function overloading (you can create two functions with different inputs but have the same name)

Speed: No slowdown -- the compiler gives functions seperate names based on their inputs, and thus differentiates by input sizes, similarities, and order of functions.
Java has this, too.

3.) Objects that syntactically encapsulate both objects and operations. C has objects that encapsulate other objects, but not operations ... they can link to operations and functions, though.

Speed: (NO slowdown !)
Java has this, too. (In fact, in java, you cannot define functions outside of objects)

4.) More syntax and grammar for dynamic memory allocation:

This is where most of the danger comes in: the slowdown occurs when someone decides to use a lot of dynamic memory. C++, like C, gives you the option to allocate memory to objects from the system, and even from other objects. Allocating memory from the system is slow, due to memory management. C++ also allows you to delete objects, and even specify how each is allocated and/or deleted.
Java is ALL ABOUT dynamic memory: unlike C and C++, Java does NOT allow you to also statically allocate composite objects.

5.) Object inheritance (A form of creating composite objects off of existing composite objects)
You can create objects as a sort of subclass of another object, that "inherit" pieces of code and data types held by this "superclass" object. In this way, you can build a tree of objects that acts like a species heirarchy, where any subclass is also an instance of its superclass. Thus if you made an object of type "music cd" and it has a superclass of type "music storage" then, by specifying the context, you can treat your object as a music CD OR as music storage in C++/java. But a "music cassette" and "music CD" would not be the same unless you viewed them from the context of music storage. (But it gets more complicated in that you can inherit multiple items ... that is, you could have a setup where a DVD would be of types "DIGITAL" and "VIDEO" whereas a video tape would be of types "ANALOG" and "VIDEO" ... they're both video objects, but their data formats differ. So they're not really classes so much as contexts ... but I didn't make the language, so I wouldn't know the reasoning behind it. :-D

This is where the second half of the slowdown takes place : inheritance hides object size. In C++ and java, if you had the above heirarchy and created a dvd object, you'd also implicitly be making a hiden digital object and video object, and whatever memory they allocate.

There are certainly many other things in different languages that slow them down. For instance Ralph Griswold's Icon has arbitrary number sizes ... i.e. if you had enough processing power and computer speed, you could calculate pi to the 999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999th decimal point easily, and store it in an Icon primitive1 floating point2 type.
The downside, of course, is that it's SLOW !

Take Icon's long number handling and create even a 14-bit RSA3program, (the standard is around 128 bits or so. follow the explanation at the bottom(3) to find out what that means.) and compare its speed with one made in SML (Standard Meta Language) and while the average implementation in SML runs within a second, Icon's average implementation runs between a minute and 20 minutes, depending on which language features are used and how well the algorithm is coded ... but part of this can be due to how good the compiler is. The compiler takes source code written by a programmer and turns it into a program. (that is, it translates source code into machine language.) Now a good compiler will optimize the code in a way that allows the computer to read it faster and with less memory ... in other words, quickly and concisely. So, say if Icon was ported to GCC 4, etc, etc, etc ... but then, we could conjecture the same "what if" to SML.)

So what can be said of all this ? Java's new features are really nice. The C programming language is perfect for low-level programming, and C++ can do everything C can do, and if you're careful, you can still use C++'s extra features without noticeable slowdown. Finally, since there are weird quirks to all languages, there may yet be some things that are actually done faster in various languages than other even if such languages are "high-level".

1-Primitive: in programming, this defines a type of variable/container (like a math variable) that is built into the language, and is used "like a lego block" to create other data types. Primitives are traditionally easy to create, usually from a standard "stack" of memory set aside especially for these "primitive" data types, and are automatically destroyed when no longer in use/accessible.
2Floating-point: this defines a term in mathematics used to describe numbers with fractions, (i.e. -- numbers that have a "floating point," like 3.2 or 241.002351 and even 2.000 )
3RSA - A (computation-intensive) form of public key cryptography introduced by Rivest, Shamir, and Adleman - 1977. In cryptography, the number used in front describes how many possible guesses one would have to make in order to unlock encrypted data. This number is known in mathematics as the key space. In this case, the key space is 14-bits long, where 14 is an exponent, and "bit" describes the counting base raised to the power of that exponent :

bit:binary = 2 [0 1]
octet:octal = 8 [0 - 7]
digit:decimal = 10 [0 - 9]
hex-digit:hexidecimal = 16 [ 0 1 2 3 4 5 6 7 8 9 a b c d e f ]

In this case it's two, so all they're really saying is that there are less than
214(16384) combinations. So, in general, a higher bit-size is associated with better protection, but that's not necessarily true.
4GCC - The GNUC programming languge Compiler. (GNU: GNU is Not Unix - the definition stems from a joke regarding recursive acronyms) GCC is an incredible, legally free (and protected), feature-packed, legally-hackable/modifiable, multi-language compiler of cult status among programmers who enjoy the GNU free software foundation. However, many people like to polarize the status into Microsoft vs. GNU, because Microsoft's Visual Studio.NET is the "GCC" of Windows (Even though GCC is also available for Windows, runs java, and there are multiple ports for .NET available, too), yet Microsoft's has automatic memory management and is bundled with "beautifully visual" tools that make programming much faster and produces more aesthetic results at the cost of bulkiness, slowdowns at odd times for no apparent reason, and a layer of abstraction that makes it difficult to know what third-party operations take place while your own code runs, and whether a particular function is optimized. It's also impossible to directly change memory locations. The efficiency and ease, while too advantageous to ignore, also allows for people with insufficient knowledge to create working programs. While a good thing, (beginners can learn the ropes while still producing significant results) these fancy tools won't stop you from writing bad code. So it's bad for job security and industry, but a professional-looking user interface made in one hour, for a complex program made in 240 hours, can make all the difference.


1 The Free Software Foundation
2 Java
3 Microsoft's .NET platform
4 .GNU: .NET compatibility for GCC
5 The SML/NJ programming language
7 The Icon programming language
8 java to c++ and vice versa converter (only does certain window and utility libraries, but that's still enough!)

9 Lamda- a place to discuss programming languages... just found this place and I think it rocks.