Is it faster to use Python or Java data structures?

I am trying to figure out whether and under what terms to use the Python and / or Java classes.

If you are creating a specialized dictionary / class map type, should there be one subclass from a Python dict or from a Java HashMap or TreeMap, etc.?

It is tempting to use Python just because they are simpler and sexier. But one of the reasons Jython is relatively slow (as it seems to me) seems to be related to dynamic typing. I’d better say that I don’t understand all this, and didn’t spend the night hours sending it to the Python / Jython interpreter code, to my shame.

In any case, it just occurs to me that Java classes can work faster because this code may need less work. OTOH, perhaps he should do more. Or maybe this is nothing. Somebody knows?

+6
source share
2 answers

As a rule, the solution should not be one of the fastest - Python classes will be implemented in terms of Java classes in any case, even if they do not inherit from them. Thus, the speed should be approximately comparable, and, at most, you would save a couple of method calls per operation.

The bigger question is what you plan to do with your class. If you use it with the Python APIs, you'll want to use Python types or something that behaves like them, so you don't have to do the work of implementing the entire matching protocol (only the bits of your class are changed). If you use Java APIs, you definitely need to perform static type checks, which means you need to inherit Java classes.

If it’s not easy for you to answer in your situation, start with Python, as you (correctly ;-) will find them “simpler and sexier”. If your class does not go beyond your project, then it should be trivial to change later, if speed really becomes a problem - and at this point you can also think about issues such as “can this help to implement it” completely at the level Java? "which we hopefully find out will be premature optimization to think about it now.

+4
source

The point of using Jython is that you can write Python code and run it on the JVM. Don't ruin it by turning your Python into Java.

If - , if - it turns out that your data structure is too slow, you can replace it with a version of Java. But this is for the programming optimization phase that comes later.


I think I should try to answer your question. I would suggest that using native Java structures would be faster (because the JVM can infer more about them than the Python interpreter), but this could be balanced by the extra processing needed to interact with Jython. There will be only tests!

+5
source

Source: https://habr.com/ru/post/918874/


All Articles