If I compile code on a specific machine/OS, will this code run better on this same machine than on a different one?
Would a java application programm compiled on Windows run without performance issue on a different OS ? If so, is there an OS to prefer to compile and get maximum performance?
CodePudding user response:
The performance of java binaries is determined by the code itself and by the implementation of the JVM (Java Virtual Machine).
Imagine the JVM something of a computer in your computer. So no, it's always the same. And that's one of the Java advantages
CodePudding user response:
TL;DR: Generally speaking, there is no such performance impact. Compile where you like, using the compiler you like. Then, when running the program, choose the "best" JRE for this machine and OS.
Java compiles to bytecode, which is a kind of abstract, high-level machine language. And in many situations, it's quite obvious which bytecode instructions have to be issued to represent some line of Java code. So, the output of different Java compilers will typically be very similar, and that's why the compiler and gthe compilation OS effectively don't matter.
If there are any (minor) differences, they apply to the specific compiler used, and do not depend on the OS where that compiler runs.
The greatest performance impact comes from the Java Runtime Environment, as this component takes the bytecode input and (in the performance-relevant situations) translates it into machine instructions (Just-In-Time compilation). There have been permanent improvements on this part, so you can expect the very same compiled Java program to perform much better under a current JRE, compared to e.g. an old Java 1.5.
The OS where the code runs is nearly irrelevant for the performance, as it's mainly machine code that gets executed, not OS library calls. What's important, is the machine architecture. So, e.g. on an ARM-based machine, you might want to choose a different JRE vendor than for an x86 machine.
CodePudding user response:
Short answer: no, the bytecode will be the same independently on the O.S. you compile the code onto. However, the same bytecode may perform faster/slower on a different O.S.
Long answer:
- When you compile your Java source code you're translating it into bytecode. No matter the platform you compile on, the bytecode will be the same. So the answer to your question is no, it's not because you compile your Java code on Windows that it will be more performant on Windows.
- At execution time, the same bytecode is fed to the platform-specific implementation of the JVM. If you run on Windows, the JVM will translate the bytecode into Windows-understandable instructions and will perform it. Same for the other O.S.
Now, there is still something you have to consider which is that your Java code may become slower on some platforms.
Imagine that you write some code that is supposed to retrieve some file from the File System and read it.
I'll use a standard example about reading a file in Java:
private String readFile(String fileName)
throws IOException {
StringBuilder builder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
builder.append(bufferedReader.readLine()).append(System.lineSeparator());
}
return builder.toString();
}
The above code will run on any platform. Also, the bytecode produced will be the same no matter the platform you compile it into.
However, when it's time to run it on a specific platform, operations such as finding the file in the file system, opening it for read, streaming the bytes etc. strictly depend on how the Operating System does that. Hence, the same code snippet may perform faster on some O.S. (because the O.S. is natively faster than another to do a specific operation, or because the JVM is especially optimized for that, or a mix of both).
Ironically, if your code performs operations that are faster on Linux than Windows, then you may compile your code on Windows and still being faster on Linux than Windows itself. That should give you the taste of how uncorrelated are the compilation and the execution.
So no, the code is not faster/slower because you compile it on one platform rather than the other. But the same code may be faster/slower on a specific O.S. because of what it does and how the low-level operations are implemented both in the target JVM and the target O.S.