简体   繁体   English

Jython - 使用Python数据结构或Java数据结构更快吗?

[英]Jython - is it faster to use Python data structures or Java ones?

I'm trying to understand whether and under what circs one should use Python classes and/or Java ones. 我试图了解是否应该使用Python类和/或Java类。

If making a specialist dictionary/Map kind of class, should one subclass from Python's dict, or from Java's HashMap or TreeMap, etc.? 如果制作一个专业词典/ Map类的类,应该是Python的dict中的一个子类,还是来自Java的HashMap或TreeMap等?

It is tempting to use the Python ones just because they are simpler and sexier. 很容易使用Python,因为它们更简单,更性感。 But one reason that Jython runs relatively slowly (so it appears to me to do) seems to have something to do with the dynamic typing. 但Jython运行相对缓慢的一个原因(我觉得这样做)似乎与动态类型有关。 I'd better say I'm not that clear about all this, and haven't spent nocturnal hours poring over the Python/Jython interpreter code, to my shame. 我最好说我对这一切并不是那么清楚,并且没有花费数小时的时间仔细研究Python / Jython解释器代码,这让我感到羞耻。

Anyway it just occurs to me that the Java classes might possibly run faster because the code might have to do less work. 无论如何,我发现Java类可能运行得更快,因为代码可能需要做更少的工作。 OTOH maybe it has to do more. OTOH也许它必须做得更多。 Or maybe there's nothing in it. 或许其中没有任何内容。 Anyone know? 谁知道?

The point of using Jython is that you can write Python code and have it run on the JVM. 使用Jython的关键是你可以编写Python代码并让它在JVM上运行。 Don't ruin that by making your Python into Java. 不要通过将Python变成Java来破坏它。

If -- if -- it turns out that your data structure is too slow, you can drop-in replace it with a Java version. 如果 - 如果 - 结果表明您的数据结构太慢,您可以直接用Java版本替换它。 But that's for the optimisation stage of programming, which comes later. 但这是编程的优化阶段,后来会出现。


I guess I should try to answer your question. 我想我应该尝试回答你的问题。 I would guess that using native Java structures will be faster (because the JVM can infer more about them than the Python interpreter can), but that might be counterbalanced by the extra processing needed to interface with Jython. 猜想使用本机Java结构会更快(因为JVM可以比Python解释器更多地推断出它们),但这可能与Jython接口所需的额外处理相抵消。 Only tests will tell! 只有测试才能说明!

Generally, the decision shouldn't be one of speed - the Python classes will be implemented in terms of Java classes anyway, even if they don't inherit from them. 通常,决策不应该是速度之一 - 无论如何,Python类都将以Java类的形式实现,即使它们不从它们继承。 So, the speed should be roughly comparable, and at most you would save a couple of method calls per operation. 因此,速度应该大致相当,并且每次操作最多可以节省几个方法调用。

The bigger question is what you plan on doing with your class. 更大的问题是你计划在课堂上做什么 If you're using it with Python APIs, you'll want to use the Python types, or something that behaves like them so that you don't have to do the work of implementing the entire Mapping protocol (only the bits your class changes). 如果您将它与Python API一起使用,您将需要使用Python类型或类似行为的东西,这样就不必执行整个映射协议的工作(只有您的类更改的位) )。 If you're using Java APIs, you will certainly need to meet the static type checks - which means you'll need to inherit from Java's classes. 如果您正在使用Java API,那么您肯定需要满足静态类型检查 - 这意味着您需要继承Java的类。

If this isn't easy to answer in your situation, start with the Python ones, since you (correctly ;-) find them "simpler and sexier". 如果在你的情况下这不容易回答,那就从Python开始,因为你(正确地;-)发现它们“更简单,更性感”。 If your class doesn't pass outside the boundaries of your project, then this should be trivial to change later if the speed really becomes an issue - and at that point, you might also be thinking about questions like "could it help to implement it entirely at the Java level?" 如果你的课程没有超出你项目的范围,那么如果速度确实成为一个问题,那么以后应该改变这一点应该是微不足道的 - 那时你也可能会考虑诸如“它是否有助于实现它”之类的问题完全在Java级别?“ which you've hopefully recognised would be premature optimisation to think about now. 你有希望认识到的是现在想想的过早优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM