使用Chaquopy将大型数据数组从Python返回给Java

huangapple go评论71阅读模式
英文:

Returning large data array from Python to Java using Chaquopy

问题

以下是翻译好的内容:

什么是使用Chaquopy从Python返回大型数据数组到Java的最快和最佳方法?Python数据集是一个包含大约100,000个整数的整数列表(例如list = [1,3,5,7...])。

我发现我可以按以下方式将列表返回给Java:

short[] data2 = py.getModule("main").callAttr("rula_timeline").toJava(short[].class);

然而,在我的Java代码中,我需要将数据集作为ArrayList。我对Java不太了解,所以不知道如何进行转换。

英文:

What is the fastest and best way to return a large data array from Python to Java using Chaquopy? The Python dataset is a list of integers (e.g. list = [1, 3, 5, 7...]) with a size of +100,000.

I found that I could return the list to Java as following:

short[] data2 = py.getModule("main").callAttr("rula_timeline").toJava(short[].class);

However, in my Java I need the dataset as an ArrayList. I am new to Java so I don't know how to convert it.

答案1

得分: 0

当然,关于如何将数组转换为ArrayList的问题在StackOverflow上已经有了,所以我就不在这里重复了。

然而,如果你正在处理成千上万项的列表,在从Python转换为Java时可能会遇到一些性能问题。在Chaquopy 8.0.0及更高版本中,可以通过使用相应类型的NumPy数组,而不是普通的Python列表来解决这个问题。例如,如果你想要转换为Java的short数组,在追求最佳性能时,你的Python代码应该返回一个NumPy的int16数组。

英文:

Of course there's already a StackOverflow question on how to convert an array to an ArrayList, so I won't repeat that here.

However, if you're dealing with lists of hundreds of thousands of items, then you may have some performance problems when converting from Python to Java. In Chaquopy 8.0.0 and later, this can be fixed by using a NumPy array of the corresponding type, rather than a plain Python list. For example, if you want to convert to a Java short array, then for maximum performance your Python code should return a NumPy int16 array.

huangapple
  • 本文由 发表于 2020年9月13日 18:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63869688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定