The relationship between C++ and assembly and machine code

This is a discussion on The relationship between C++ and assembly and machine code within the C++ Programming forums, part of the General Programming Boards category; I have just read that programs written in machine code or assembly are usually faster than equivalent programs written in ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    The relationship between C++ and assembly and machine code

    I have just read that programs written in machine code or assembly are usually faster than equivalent programs written in C++(or any other higher level languages). I was curious as to why this would be, since programs written in C and C++ are compiled prior to running the program. Thanks for your time.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Assembly language is so close to the machine that it can be used to create the most efficient operations possible, C++ can't quite do that due to abstraction and the overhead of the language. C can get close with extreme tweaking, but at a signifigant loss in readability.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    I thought that the speed difference from C to C++ was pretty much negligable? I know there must be some overhead from an object oriented system...but isn't it almost non existant with a good compiler? And if i'm writting proceduraly, woud't it be the same as C?
    -------------------
    "Exception"

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Thanks for the help

    Thanks for your help, Prelude. Just to make sure I have this right, assembly and machine code are faster because they you can use more precise commands? Is this similar to translating from English to Spanish and finding that there is not a word exactly equivalent, and that you will have to use a whole phrase instead?

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Assembly basically just changes the binary code into a readable form. Therefore, when you type move ax,bx , the asm compiler changes it into machine code.

    As established on a different forum, C and C++ are the same speed if the same code is used. However, once OOP is introduced (such as inheritance, ect.), the code becomes slower.

    Also, when you write in C, the compiler changes the code into machine code. Therefore, as long as your compiler is good at optimizing, C should be about the same speed as asm.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >I thought that the speed difference from C to C++ was pretty
    >much negligable?

    Yes.

    >I know there must be some overhead from an object oriented?
    >system...but isn't it almost non existant with a good compiler?

    If your compiler does good optimization, the differences in speed will be very small.

    >And if i'm writting proceduraly, woud't it be the same as C?

    Almost.

    >Just to make sure I have this right, assembly and machine code
    >are faster because they you can use more precise commands?

    When using assembly, it's almost one-to-one translation to binary code. A compiler can also do good optimisations, but a compiler doesn't know about the used algorithms. You, the programmer, are the one who knows how to implement an algorithm in assembly better than a compiler does.

    >Is this similar to translating from English to Spanish and finding
    >that there is not a word exactly equivalent, and that you will
    >have to use a whole phrase instead?

    Yes. When translating from one programming language to a different language requires rewriting whole functions (paragraphs), not only phrases. Note that there are cases where it is much harder. Translating Pascal to C is much easier than translating C++ (when object oriented) to C or Haskell (a functional language) to C.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21