简体   繁体   中英

Does Python/Java program work as fast as C if I convert both of them into exe?

I want to understand this difference between interpreted languages and compiled languages. Lot of explanation is found online and I understand all of them.

But question is, softwares are distributed as exe(on windows) as final products.

So if I write my program in Python/Java and compile it to exe, would that work as fast as if I had written and compiled in C/C++?

No, it will not. It is not about the file format, it is the complexity of elementary building blocks the language compiles to that matter.

In practice, most languages are compiled. Java is and python, internally in the interpreter, really is too. However, there is a difference in how complex execution of the building blocks of the target binary code are.

For C++, all the concepts are mapped directly to the low level concepts understood by the CPU. The disadvantage is that things like race conditions are undefined behaviour. The code simply does not define what should happen in various corner cases at all and it is programmer's responsibility to handle them. It is also programmers responsibility to manage resources, because there is no collector and such.

For Java, the concepts are mapped to instructions of the virtual machine. These may be interpreted (which involves some overhead decoding them), or they may be still further compiled down the machine instructions either just-in-time (as the standard desktop VM does) or ahead-of-time (as the current Android runtime does), but each will still be more complex to execute. Assignments will have lock prefixes to ensure they don't generate invalid values in race-conditions, there is the garbage collector running across the memory looking for valid pointers and copying stuff around, objects have quite a bit of extra overhead and lot of things are checked so it can be nice and throw exceptions on errors rather than crashing like C++ does.

And python is yet more complex. It does not actually run python code in parallel (except for blocking system calls) at all due to the guarantees the language provides, there is also collector, this time one that updates reference counts in each operation and objects have a lot more overhead, because most member accesses are actually hash table lookups—contrast to C++ simple load from pointer with offset. And there is even more checking of everything. Orders of magnitude speed difference. In fact, there is much bigger difference between Java and Python than there is between C++ and Java.

The compilation to machine code directly interpreted by the CPU does help a bit. But the higher level languages add overhead that is due to their semantics, not due to the fact they are interpreted.

That said, @linuxuser is correct that converting Java or Python code does not actually translate it to machine code, but simply bundles the interpreter and the code to run in a package, so there is no gain simply because there is actually no difference.

No

The reason compiled C programs run fast is because C code is compiled into assembly language, which is a language designed specifically for your CPU. Java and Python run slower because Java needs to run in a JVM and Python needs to be run by the Python interpreter, which slows things down.

There are tools available that can convert Python scripts and Java programs into Windows Executables, however these executables do not create assembly language from your Java and Python programs. Java code must be run on a JVM and Python Code must be run through the Python interpreter. You cannot "compile" a Python or Java program and get assembly language the same way you do with C. (You can actually compile Java to Native code but I'm fairly sure the tools you're referring to in this question are not ones that convert Java to native assembly code)

These tools that convert Python code and Java code to executables really just bundle the Python interpreter into the executable and use that to run your Python code, or automatically download a JVM and then use that to run your Java code.

Here's an example of a Jar to exe program . As you can see, it just creates an exe file that automatically downloads the latest JVM and then runs your java code with the downloaded JVM.

When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a web site.

Here is a tool for converting Python scripts to exe. As you can see, it just bundles the Python interpreter into an exe along with your code and runs your code with the bundled Python interpreter. Scroll down to the "What are all those files?" section.

In practice no.

The languages C/C++ were written as a better assembler. Their underlying operations have been designed to have a good fit with the processors of the 1970.

Subsequently, processors have been driven to run quickly, and so they have been designed around instructions which can make C/C++ faster.

This close linking with the semantics of the language and the silicon, has given a headstart to the C/C++ community.

An example of the C/C++ advantage, is how simple types and objects can be created on the stack. The stack is implemented as a processor stack, and objects will only be valid whilst their callstack is current.

Java/python implement all their objects on the free-store, having lambdas and closures which stretch the lifespan of their objects beyond the call-stack which create them. The free-store is a more-costly way of creating objects, and is one of the penalties the language take.

JIT compiling the java/python bytecode, can make up some of the difference, and (theoretically) beat the performance of the C/C++ code.

When JIT compiled, a language is compiled based on the processor on the box (with possibly better features than when the code was written), with knowledge of the exact data that is being used with the code. This means the Jit compiler is tuned to the exact usage of the code. Rather than a compiler's best guess.

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