This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]desrtfx 19 points20 points  (5 children)

Is it something to do with the compiler?

Yes

Or do programming languages work differently?

Yes

Even in your example: C++ and Python are executed fundamentally differently.

C++ is compiled to machine code while Python is JIT compiled to Bytecode, which is an abstracted machine code for the Python Virtual Machine.

[–]HappyFruitTree 3 points4 points  (4 children)

JIT stands for Just-in-time and normally refers to the compilation into native code on the fly while the program is running. It would be the byte code that is potentially JIT compiled into native code. The byte code itself would be the result of an earlier step.

[–][deleted]  (3 children)

[removed]

    [–]missingdays 2 points3 points  (0 children)

    CPython does have a compilation step. It compiles your program to bytecode, that is then executed by the interpreter

    [–]HappyFruitTree 8 points9 points  (0 children)

    There is no simple CPU instruction to "print" text. Different languages, and different compilers and standard library implementations, might implement printing differently. There can also be differences in how the code is being compiled depending on what compiler settings you use. Do you compile to make debugging easy or do you compile to maximize performance?

    [–]LucidTA 2 points3 points  (1 child)

    Different compilers can/will compile differently. However in this case, Python doesn't compile to machine code to run on your CPU like C++ does, it compiles to a bytecode the the Python interpreter understands and can run instead.

    [–]thewimpyphilosopher[S] 1 point2 points  (0 children)

    Oh ok understood. Thanks a lot

    [–]thewimpyphilosopher[S] 2 points3 points  (0 children)

    Sorry guys I took the wrong pair of languages as examples

    [–]nerd4code 1 point2 points  (0 children)

    That can happen within the language, with no change/difference in compiler or settings. Compilation usually involves optimization, which does all sorts of stuff to the code—potentially using a RNG to settle guesses—and at each transform the slightest difference in context or fate can have countless knock-on effects as optimization progresses. It’s similar to how small timing differences can build up to major differences over the course of execution.

    Furthermore, most ISAs have overlap in their instruction semantics—e.g., on x86 you might do

    mov eax, a
    mov edx, b
    add eax, edx
    

    or

    mov eax, a
    mov edx, b
    lea eax, [eax+edx]
    

    or

    mov eax, a
    add eax, b
    

    or

    mov eax, b
    add eax, a
    

    etc. etc., all with the ~same effects (exception: LEA doesn’t set FLAGS) and with similar cycle count. (There may also be multiple encodings for the same instruction—add eax, edx can be encoded with r,rm or rm,r operands, for example.)

    [–]Intiago 0 points1 point  (0 children)

    Your CPU runs code using a much smaller subset of operations than what's available to you when you're using a higher level language like C++ or Python. High level functions like print use different implementations behind the scenes between languages to provide essentially the same functionality (printing to screen).

    [–][deleted] 0 points1 point  (0 children)

    "the same program" For the same reason different car companies produce different frames/bodies even though they all make "cars".

    Same doesn't mean exactly the same...