4 ms·
Java is slower than Python for simple short-running programs. When Python is finished Java is still struggling through start-up and class-loading.
by ptx 7y ago
Java is slower than Python for simple short-running programs. When Python is finished Java is still struggling through start-up and class-loading.
- twic 7y agoIs this actually true? Have you measured this, or seen measurements?
- ptx 7y agoYes, I often write small command-line tools and that's what I found. Profiling seemed to indicate that doing anything at all in Java, such as reading a config file, is fast the second time but super slow the first time. You can test my assertion simply by writing a "hello world" in Python and Java.
- ptx 7y agoHere are measurements for an extreme case of a short-running simple program: $ cat Hello.java class Hello { public static void main(String[] args) { System.out.println("Hello from Java!"); } } $ cat hello.py print("Hello from Python!") $ time /usr/lib/jvm/java-13-openjdk/bin/java -Xshare:on -XX:+TieredCompilation -XX:TieredStopAtLevel=1 Hello Hello from Java! real 0m0.102s user 0m0.095s sys 0m0.025s $ time python3 -S hello.py Hello from Python! real 0m0.034s user 0m0.020s sys 0m0.013s It's a bit faster if you create a custom modular JRE with jlink: $ /usr/lib/jvm/java-13-openjdk/bin/jlink --add-modules java.base --output /tmp/jlinked-java13-jre $ /tmp/jlinked-java13-jre/bin/java -Xshare:dump $ time /tmp/jlinked-java13-jre/bin/java -Xshare:on -XX:+TieredCompilation -XX:TieredStopAtLevel=1 Hello Hello from Java! real 0m0.087s user 0m0.050s sys 0m0.035s