简体   繁体   中英

C++ architecture : how is it similar to machine architecture

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.

It's not about being "similar" in any way. It's about being close to the hardware. Neither C nor C++ hide any of the nitty-gritty details of memory management from you. Because you have to think about such low-level things as alignment, contiguous access, and the stack, you can write far more efficient code. Higher-level languages hide these things from you, which makes for a nicer programming experience, but often times the code isn't nearly as optimized as it could be.

The evolution of programing languages indicates that the more convenience you have in programming,the more tradeoff in performance you need to pay for that. For most programming beginners,Java is definitely more friendly to them than c or c++.(memory management,header files,containers,etc ). C or C++ is faster than Java because they are compiled into machine code,which can be loaded and run directly.But for java,you may know about JRE,which is a run-time framework for java code,as the Java code compiled into some code that only understood by Java Virtual Machine(which is written by c with a little assembly).The .Net framework to C# is as JRE to Java. This difference in performance may acts like a big deal in some performance-oriented business but for most applications,this is hardly visible. Hope that explains!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM