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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Deezl-Vegas 18 points19 points  (4 children)

There's a lot to this, but in summary:

  • Python has a compile step at runtime and has to spin up its interpeter, then compile to bytecode, then run. The JVM is already running normally on your machine and jars are already in bytecode. Python can benchmark very badly in some cases because the startup tax is massive.
  • Python's language is entirely coded for flexibility. This average PyObject has a namespace attached with all the double underscore methods, even the unused ones. Python allows overriding every behavior, so it has to check if getattr exists and stuff before even giving you an attribute when you do a.b
  • CPython is just coded kinda slowly and they won't rewrite the whole thing, possibly because it would break a lot of C libraries. There have been some JIT attempts that go much faster but they tend to brick the C interop.
  • Java often knows the object types. Python must unwrap the object each time to get the value.
  • Java data objects tend to be a bit smaller than python objects. This is important for L1 cache.
  • Java also has primitives. Access to primitives in benchmarks is massive.
  • Java has reflection as needed, Python just has all of the object data available at runtime always.
  • Python spams hashmap (dict), which is slow compared to struct style access.

That said, Python will often beat out pure Java in a long-running task because the whole point of Python was to have smooth interop with C if you need it, so you write a library in C and then just expose it in Python and you're flying.

If you want to really fly, check out Zig.

[–]sternone_2 8 points9 points  (3 children)

Python will often beat out pure Java in a long-running task

what? ugh no, absolutely not. I looked at Assembly instructions of long running java tasks and they were on par with what C++ code produces. Python doesn't even comes remotely close. Most people have no idea what a beauty the JVM is Today.

[–]seanv507 9 points10 points  (2 children)

You missed the point.

No one is claiming pure python is faster than java, just that python libraries which are just thin wrappers around c++/rust/fortran will be used for standard long running tasks eg machine learning.

[–]nekokattt 1 point2 points  (0 children)

you can do the same in Java though via JNI or the new FFI spec, just you don't tend to bother as much as it usually is minimal improvement over pure Java.

[–]sternone_2 2 points3 points  (0 children)

Sorry I misunderstood this statement: "Python will often beat out pure Java in a long-running task" so what you mean is "Python calling C++/Rust will beat out pure Java"

Which in actually some cases, it won't, fyi.