So all I’ve ever programmed in so far is C++, and I commonly see people mention that the reason C++ is fast is because it’s so similar to machine code. I’m wondering what exactly machine/C++ code architecture is, why being similar makes it faster, and how it compares to other architectures such as C#.
I understand RAII, the heap, stack, and syntax but this is about it. I don’t even have that good of an understanding of what a computer is made up of other than CPU, RAM, and hard drive. I’m planning on starting to learn C# soon for WP7 app development soon, so I assume a deeper understanding of what’s going on under the hood would help me identify the differences/similarities between the languages.
Diagrams or pointers to articles on the subject would be great!
The stuff that makes C++, C++, is not really similar to any machine architecture. It’s the C subset that’s similar. Variable assignments, function calls, loops, array traversal, and comparisons all tend to map to a small set of fast, core instructions. Assignments to primitives or to array elements are generally a single instruction on modern processors: an instruction that moves a value from memory to a register, or vice-versa (for example.) Folks who have been programming in C a long time can literally see the machine code their compiler will generate. The key thing is “what you see is what you get” — the language instructions translate directly into machine code, more or less one-to-one.
On the other hand, “higher-level” languages like C# (or Java, Ruby, Python, Perl, Haskell, Scheme, etc) have a more or less substantial underlying runtime support system. For some languages, that means an assignment might involve looking something up in a table first; for others, it might mean that an assignment is a simple copy sometimes, or a complex data manipulation other times, depending on what sort of data it is. It’s much harder to predict how your statements will be translated into machine code.
C++ is in an interesting middle ground: some assignments are just like C assigments; other are actually overloaded calls to
operator=(), and you’re never quite sure what you’re going to get (without close study, of course.) C++ does have a runtime system; it’s just a lot lighter weight than the one for Ruby or Haskell or Scheme.