简体   繁体   English

JNI:是否可以避免在JNI中创建很多Java对象?

[英]JNI : Is it possible to avoid create a lot of Java object in JNI?

I want to improve performance of my code here: 我想在这里提高代码的性能:

    jclass segWordClazz = env->FindClass("xxx/yyy/SegWord");
    jmethodID initMethodId = env->GetMethodID(segWordClazz, "<init>", "(Ljava/lang/String;II)V");

    jsize size = (jsize) words.size();
    jobjectArray arr = env->NewObjectArray(size, segWordClazz, NULL);
    jint i = 0;
    for (i = 0; i < size; i++) {
        jstring word = env->NewStringUTF(words[i].word.c_str());
        jobject obj = env->NewObject(segWordClazz, initMethodId, word,
                                     (jint) words[i].unicode_offset,
                                     (jint) words[i].unicode_offset + words[i].unicode_length);

        env->SetObjectArrayElement(arr, i, obj);
        env->DeleteLocalRef(obj);
        env->DeleteLocalRef(word);
    }

What I am trying to do is, split a text into words, but if the text is too long, it may generate too many words that cause a lot of NewObject allocation (1 or 2 million maybe more). 我想做的是将一个文本分割成多个单词,但是如果文本太长,它可能会生成太多单词,从而导致大量NewObject分配(可能会增加1或200万个)。

I was wondering if it possible to improve the performance here? 我想知道是否可以改善此处的性能? This piece of code takes too much time to execute. 这段代码花费太多时间来执行。

Your code is sequentially iterating and processing some input. 您的代码将依次迭代和处理一些输入。

I do not see how you could reduce the object footprint (as I do not know your requirements - you don't explain why you are doing things exactly this way). 我看不到如何减少对象占用空间(因为我不知道您的要求-您没有解释为什么要完全按照这种方式做事)。

But regarding the overall runtime: consider "slicing" your input data into several "partitions - that would enable you to run things in parallel. That would even increase memory consumption, but should allow for faster processing. 但是关于整体运行时:考虑将输入数据“切片”为几个“分区”,这将使您能够并行运行事物。这甚至会增加内存消耗,但应允许更快的处理速度。

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

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